2024-03-22 13:51:30 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
2024-03-28 18:09:28 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2024-03-22 13:51:30 +08:00
|
|
|
import 'package:jpush_flutter/jpush_flutter.dart';
|
2024-06-01 13:35:03 +08:00
|
|
|
import 'package:star_lock/flavors.dart';
|
2024-05-27 17:41:01 +08:00
|
|
|
import 'package:star_lock/mine/minePersonInfo/minePersonInfoEditAccount/minePersonInfoEditAccount/mineUnbindPhoneOrEmail_entity.dart';
|
2024-03-22 13:51:30 +08:00
|
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
2024-06-15 13:35:23 +08:00
|
|
|
import 'package:star_lock/tools/push/message_management.dart';
|
2024-03-22 13:51:30 +08:00
|
|
|
import 'package:star_lock/tools/storage.dart';
|
|
|
|
|
|
2024-06-15 13:35:23 +08:00
|
|
|
import '../../app_settings/app_settings.dart';
|
2024-04-26 15:38:59 +08:00
|
|
|
|
2024-03-22 13:51:30 +08:00
|
|
|
class XSJPushProvider {
|
|
|
|
|
final JPush jpush = JPush();
|
|
|
|
|
|
2024-05-27 17:41:01 +08:00
|
|
|
// appKey: 251fc8074820d122b6de58d2--鑫泓佳AppKey
|
|
|
|
|
// appKey: 7ff37d174c1a568a89e98dad--sky
|
2024-03-22 13:51:30 +08:00
|
|
|
Future<void> initJPushService() async {
|
2024-05-27 17:41:01 +08:00
|
|
|
final String? data = await Storage.getString(saveUserLoginData);
|
|
|
|
|
if (data == null || data.isEmpty) {
|
|
|
|
|
AppLog.log('No user data found.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 13:35:03 +08:00
|
|
|
String appKey;
|
|
|
|
|
if (F.isSKY) {
|
|
|
|
|
appKey = '7ff37d174c1a568a89e98dad';
|
|
|
|
|
} else {
|
|
|
|
|
appKey = '251fc8074820d122b6de58d2';
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 17:41:01 +08:00
|
|
|
jpush.setup(
|
2024-06-01 13:35:03 +08:00
|
|
|
appKey: appKey,
|
2024-05-27 17:41:01 +08:00
|
|
|
channel: 'flutter_channel',
|
|
|
|
|
production: false,
|
2024-06-15 15:14:50 +08:00
|
|
|
debug: false,
|
2024-05-27 17:41:01 +08:00
|
|
|
);
|
2024-03-22 13:51:30 +08:00
|
|
|
|
2024-05-29 17:31:55 +08:00
|
|
|
jpush.applyPushAuthority(
|
|
|
|
|
const NotificationSettingsIOS(sound: true, alert: true, badge: false),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
addJPushEventHandler();
|
2024-06-25 10:44:26 +08:00
|
|
|
AppLog.log('JPush initialized.');
|
2024-05-29 17:31:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//极光推送事件处理方法
|
|
|
|
|
void addJPushEventHandler() {
|
2024-05-27 17:41:01 +08:00
|
|
|
jpush.addEventHandler(
|
2024-09-04 09:09:40 +08:00
|
|
|
onReceiveNotification: (Map<String, dynamic> message) async {
|
|
|
|
|
AppLog.log('onReceiveNotification: $message');
|
|
|
|
|
},
|
|
|
|
|
onOpenNotification: (Map<String, dynamic> message) async {
|
|
|
|
|
AppLog.log('onOpenNotification: $message');
|
|
|
|
|
},
|
|
|
|
|
onReceiveMessage: (Map<String, dynamic> message) async {
|
|
|
|
|
AppLog.log('onReceiveMessage: $message');
|
|
|
|
|
//这里接收自定义消息
|
|
|
|
|
MessageManagement.shunting(message);
|
|
|
|
|
},
|
|
|
|
|
onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
|
|
|
|
|
AppLog.log('onReceiveNotificationAuthorization: $message');
|
|
|
|
|
},
|
|
|
|
|
onInAppMessageShow: (Map<String, dynamic> message) async {
|
|
|
|
|
AppLog.log('onInAppMessageShow: $message');
|
|
|
|
|
},
|
|
|
|
|
onConnected: (Map<String, dynamic> message) async {
|
|
|
|
|
//绑定设备id
|
|
|
|
|
final String rid = await jpush.getRegistrationID();
|
|
|
|
|
AppLog.log('onConnected registration id : $rid');
|
|
|
|
|
await Storage.setString(pushDeviceID, rid);
|
|
|
|
|
await pushBindDeviceID(rid, Platform.isAndroid ? 10 : 20);
|
2024-03-22 13:51:30 +08:00
|
|
|
|
2024-09-04 09:09:40 +08:00
|
|
|
return Future.value();
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-03-22 13:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 17:41:01 +08:00
|
|
|
Future<void> pushBindDeviceID(String deviceID, int deviceType) async {
|
|
|
|
|
try {
|
|
|
|
|
final MineUnbindPhoneOrEmailEntity entity =
|
|
|
|
|
await ApiRepository.to.pushBindAppId(deviceID, deviceType);
|
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
AppLog.log('绑定成功');
|
|
|
|
|
} else {
|
|
|
|
|
AppLog.log('绑定失败');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
AppLog.log('Error binding device ID: $e');
|
2024-03-28 18:09:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 18:04:17 +08:00
|
|
|
//本地通知初始化 isCancelLocalPush 是否取消所有本地通知
|
2024-05-27 17:41:01 +08:00
|
|
|
Future<void> initLocalNotification({required bool isCancelLocalPush}) async {
|
2024-03-28 18:09:28 +08:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
|
|
|
FlutterLocalNotificationsPlugin();
|
2024-05-27 17:41:01 +08:00
|
|
|
|
2024-04-16 15:05:35 +08:00
|
|
|
if (Platform.isAndroid) {
|
2024-05-27 17:41:01 +08:00
|
|
|
const AndroidInitializationSettings initializationSettingsAndroid =
|
|
|
|
|
AndroidInitializationSettings('app_icon');
|
2024-04-16 15:05:35 +08:00
|
|
|
const InitializationSettings initializationSettings =
|
|
|
|
|
InitializationSettings(android: initializationSettingsAndroid);
|
|
|
|
|
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
|
|
|
|
|
}
|
2024-03-28 18:09:28 +08:00
|
|
|
|
|
|
|
|
if (isCancelLocalPush) {
|
|
|
|
|
await flutterLocalNotificationsPlugin.cancelAll();
|
|
|
|
|
}
|
2024-03-22 13:51:30 +08:00
|
|
|
}
|
|
|
|
|
}
|