Files
tuiche/lib/common/util/JPushUtil.dart
2025-09-11 09:38:14 +08:00

197 lines
6.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'dart:io';
import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:jpush_flutter/jpush_interface.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
import 'package:vbvs_app/common/util/NotificationRouteManager.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
class JPushUtil {
static final JPushFlutterInterface _jpush = JPush.newJPush();
static final GetStorage box = GetStorage();
/// 初始化极光推送
static Future<void> initJPush() async {
// 先申请通知权限
await _requestNotificationPermission();
// 初始化 JPush
_jpush.setup(
appKey: 'ef31d487137311ed3d63ea7d', // 替换为你在极光控制台获取的 appKey
channel: 'developer-default',
production: true,
debug: false,
);
// 获取 RegistrationID
_jpush.getRegistrationID().then((rid) {
EasyDartModule.logger.info("[消息推送]flutter get registration id: $rid");
print("flutter get registration id : $rid");
box.write('rid', rid);
});
// 添加推送消息回调
_jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("接收到通知: $message");
EasyDartModule.logger.info("[消息推送]接收到通知: $message");
},
onOpenNotification: (Map<String, dynamic> message) async {
print("通知打开: $message");
EasyDartModule.logger.info("[消息推送]通知打开: $message");
openAppRouteByNotification(message);
},
onReceiveMessage: (Map<String, dynamic> message) async {
print("接收到自定义消息: $message");
EasyDartModule.logger.info("[消息推送]接收到自定义消息: $message");
},
);
}
/// 请求通知权限
static Future<void> _requestNotificationPermission() async {
if (Platform.isIOS) {
_jpush.applyPushAuthority(const NotificationSettingsIOS(
sound: true,
alert: true,
badge: true,
));
} else if (Platform.isAndroid) {
var status = await Permission.notification.status;
if (status.isDenied) {
await Permission.notification.request();
}
}
}
/// 根据通知跳转页面
// static Future<void> openAppRouteByNotification(
// Map<String, dynamic> message) async {
// try {
// int type = message['extras']['cn.jpush.android.EXTRA']['mType'];
// if (type == 1) {
// var person =
// jsonDecode(message['extras']['cn.jpush.android.EXTRA']['person']);
// String mac = person['mac'];
// UserInfoController userInfoController = Get.find();
// if (userInfoController.model.login == 0) {
// box.write("needSleepReport", "true");
// box.write("needSleepReport_person", person);
// box.write("needSleepReport_mac", mac);
// Get.toNamed("/loginPage");
// } else {
// Get.toNamed(
// "/newSleepReportPage",
// arguments: {
// 'mac': mac,
// 'type': 1,
// "person": person,
// 'backgroundImg': 'assets/images/new_background.png',
// 'date': DateTime.now().millisecondsSinceEpoch,
// 'person_show': false,
// 'reportPadding': false,
// },
// );
// }
// }
// } catch (e) {
// EasyDartModule.logger.error("[打开APP异常]:$e");
// DailyLogUtils.writeError("[打开APP异常]:$e");
// }
// }
// static Future<void> openAppRouteByNotification(
// Map<String, dynamic> message) async {
// try {
// int type = message['extras']['cn.jpush.android.EXTRA']['mType'];
// if (type == 1) {
// var person =
// jsonDecode(message['extras']['cn.jpush.android.EXTRA']['person']);
// String mac = person['mac'];
// UserInfoController userInfoController = Get.find();
// if (userInfoController.model.login == 0) {
// // 登录状态为 0先保存需要跳转的标记
// box.write("needSleepReport", "true");
// box.write("needSleepReport_person", person);
// box.write("needSleepReport_mac", mac);
// // 这里不要直接跳转,改为保存一个待跳转路由
// box.write("pendingRoute", {
// "route": "/loginPage",
// "arguments": null,
// });
// } else {
// // 已登录,保存跳转参数
// box.write("pendingRoute", {
// "route": "/newSleepReportPage",
// "arguments": {
// 'mac': mac,
// 'type': 1,
// "person": person,
// 'backgroundImg': 'assets/images/new_background.png',
// 'date': DateTime.now().millisecondsSinceEpoch,
// 'person_show': false,
// 'reportPadding': false,
// }
// });
// }
// }
// } catch (e) {
// EasyDartModule.logger.error("[打开APP异常]:$e");
// DailyLogUtils.writeError("[打开APP异常]:$e");
// }
// }
static Future<void> openAppRouteByNotification(
Map<String, dynamic> message) async {
try {
int type = message['extras']['cn.jpush.android.EXTRA']['mType'];
if (type == 1) {
var person =
jsonDecode(message['extras']['cn.jpush.android.EXTRA']['person']);
String mac = person['mac'];
UserInfoController userInfoController = Get.find();
if (userInfoController.model.login == 0) {
NotificationRouteManager().box.write("needSleepReport", "true");
NotificationRouteManager()
.box
.write("needSleepReport_person", person);
NotificationRouteManager().box.write("needSleepReport_mac", mac);
NotificationRouteManager().savePendingRoute("/loginPage");
} else {
NotificationRouteManager().savePendingRoute(
"/newSleepReportPage",
arguments: {
'mac': mac,
'type': 1,
"person": person,
'backgroundImg': 'assets/images/new_background.png',
'date': DateTime.now().millisecondsSinceEpoch,
'person_show': false,
'reportPadding': false,
},
);
}
/// 如果 APP 已经在前台,立即处理跳转
NotificationRouteManager().handlePendingRoute();
}
} catch (e) {
EasyDartModule.logger.error("[打开APP异常]:$e");
DailyLogUtils.writeError("[打开APP异常]:$e");
}
}
/// 获取 RegistrationID
static Future<String?> getRegistrationID() async {
return await _jpush.getRegistrationID();
}
}