jpush_helper.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'dart:io';
  2. import 'package:get/get.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:jpush_flutter/jpush_flutter.dart';
  6. import 'package:jpush_flutter/jpush_interface.dart';
  7. import 'package:keyboard/module/store/new_discount/new_discount_page.dart';
  8. import 'package:keyboard/utils/atmob_log.dart';
  9. import '../../data/consts/build_config.dart';
  10. import '../../data/consts/constants.dart';
  11. import '../../di/get_it.dart';
  12. import '../../module/main/main_controller.dart';
  13. import '../../router/app_pages.dart';
  14. @singleton
  15. class JpushHelper {
  16. static const String tag = "qqq JpushHelper";
  17. static String registrationId = "";
  18. static final JPushFlutterInterface _jpush = JPush.newJPush();
  19. JpushHelper();
  20. // Platform messages are asynchronous, so we initialize in an async method.
  21. Future<void> init() async {
  22. String? platformVersion;
  23. AtmobLog.d(tag, "flutter init JPushHelper");
  24. print("qqq flutter init JPushHelper");
  25. try {
  26. _jpush.addEventHandler(
  27. onReceiveNotification: (Map<String, dynamic> message) async {
  28. AtmobLog.d(tag, "flutter onReceiveNotification: $message");
  29. },
  30. onOpenNotification: (Map<String, dynamic> message) async {
  31. AtmobLog.d(tag, "flutter onOpenNotification: $message");
  32. if (Get.isRegistered<MainController>() &&
  33. Get.currentRoute != RoutePath.newDiscount) {
  34. NewDiscountPage.start();
  35. }
  36. //清除应用图标角标数量
  37. _jpush.setBadge(-1);
  38. },
  39. onReceiveMessage: (Map<String, dynamic> message) async {
  40. AtmobLog.d(tag, "flutter onReceiveMessage: $message");
  41. },
  42. onReceiveNotificationAuthorization: (
  43. Map<String, dynamic> message,
  44. ) async {
  45. AtmobLog.d(
  46. tag,
  47. "flutter onReceiveNotificationAuthorization: $message",
  48. );
  49. },
  50. onNotifyMessageUnShow: (Map<String, dynamic> message) async {
  51. AtmobLog.d(tag, "flutter onNotifyMessageUnShow: $message");
  52. },
  53. onInAppMessageShow: (Map<String, dynamic> message) async {
  54. AtmobLog.d(tag, "flutter onInAppMessageShow: $message");
  55. },
  56. onCommandResult: (Map<String, dynamic> message) async {
  57. AtmobLog.d(tag, "flutter onCommandResult: $message");
  58. },
  59. onInAppMessageClick: (Map<String, dynamic> message) async {
  60. AtmobLog.d(tag, "flutter onInAppMessageClick: $message");
  61. },
  62. onConnected: (Map<String, dynamic> message) async {
  63. AtmobLog.d(tag, "flutter onConnected: $message");
  64. },
  65. );
  66. } on PlatformException {
  67. platformVersion = 'Failed to get platform version.';
  68. }
  69. _jpush.enableAutoWakeup(enable: true);
  70. _jpush.setAuth(enable: true);
  71. _jpush.setup(
  72. appKey: JpushConfig.jpushAppKey,
  73. channel: getChannelName(),
  74. production: false,
  75. debug: BuildConfig.isDebug,
  76. );
  77. _jpush.applyPushAuthority(
  78. new NotificationSettingsIOS(sound: true, alert: true, badge: true),
  79. );
  80. // Platform messages may fail, so we use a try/catch PlatformException.
  81. _jpush.getRegistrationID().then((rid) {
  82. AtmobLog.d(tag, "极光id get registration id : $rid");
  83. print("qqq 极光id get registration id : $rid");
  84. registrationId = rid;
  85. });
  86. if (Platform.isIOS) {
  87. // iOS要是使用应用内消息,请在页面进入离开的时候配置pageEnterTo 和 pageLeave 函数,参数为页面名。
  88. // _jpush.pageEnterTo("HomePage");
  89. }
  90. isNotificationEnabled().then((hasPermission) {
  91. AtmobLog.d(
  92. "JpushHelper",
  93. ("flutter isNotificationEnabled: $hasPermission"),
  94. );
  95. if (hasPermission) {
  96. return;
  97. } else {
  98. requestRequiredPermission();
  99. }
  100. });
  101. }
  102. Future<void> setTags({required List<String> tag}) async {
  103. _jpush.setTags(tag).then((result) {
  104. AtmobLog.d("JpushHelper", ("flutter setTags: $result"));
  105. });
  106. }
  107. Future<void> cleanTags() async {
  108. _jpush.cleanTags().then((result) {
  109. AtmobLog.d("JpushHelper", ("flutter cleanTags: $result"));
  110. });
  111. }
  112. Future<bool> isNotificationEnabled() async {
  113. return _jpush.isNotificationEnabled();
  114. }
  115. void requestRequiredPermission() {
  116. _jpush.requestRequiredPermission();
  117. }
  118. void openSettingsForNotification() {
  119. _jpush.openSettingsForNotification();
  120. }
  121. static JpushHelper getInstance() => getIt.get<JpushHelper>();
  122. }