jpush.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * @Author: XianKaiQun
  3. * @LastEditors: wuwei
  4. * @Date: 2020-10-22 09:54:21
  5. * @LastEditTime: 2025-03-13 14:54:47
  6. */
  7. import 'dart:io';
  8. import 'package:flutter/material.dart';
  9. // import 'package:jpush_flutter/jpush_flutter.dart';
  10. // import 'package:permission_handler/permission_handler.dart';
  11. import 'package:wisdom_cli/wisdom_cli.dart';
  12. // export 'package:jpush_flutter/jpush_flutter.dart' show LocalNotification;
  13. typedef Future<dynamic> EventHandler(Map<String, dynamic> event);
  14. ///极光推送
  15. ///
  16. ///main函数中调用[WJpushUtil.setup],先注册极光且设置回调函数。
  17. ///
  18. ///然后再调用其他方法
  19. ///
  20. ///eg:
  21. ///在登录前后,登出前后调用相应方法操作[tags]
  22. ///
  23. ///登出后[stopPush]停止推送
  24. ///
  25. ///登录后[resumePush]恢复推送
  26. ///
  27. class WJpushUtil {
  28. WJpushUtil._();
  29. static WJpushUtil? _instance;
  30. static WJpushUtil? get instance {
  31. _instance ??= WJpushUtil._();
  32. return _instance;
  33. }
  34. bool _inited = false;
  35. late final _jpush;
  36. ///注册
  37. ///[appKey]IOS appKey
  38. ///
  39. ///[channel]channel
  40. ///
  41. ///[production]production
  42. ///
  43. ///[debug] 是否打印
  44. ///
  45. ///[sound] iOS10+ only 是否触发声音
  46. ///
  47. ///[alert] iOS10+ only 是否前台展示
  48. ///
  49. ///[badge] iOS10+ only 是否设置应用角标 badge
  50. ///
  51. ///[notificationSettingsIOS]Ios配置
  52. ///
  53. ///[onReceiveNotification]接收通知回调方法。
  54. ///
  55. ///[onOpenNotification]点击通知回调方法。
  56. ///
  57. ///[onReceiveMessage]接收自定义消息回调方法。
  58. ///
  59. void setup({
  60. required String appKey,
  61. String? channel,
  62. bool production: false,
  63. bool debug: true,
  64. bool sound = true,
  65. bool alert = true,
  66. bool badge = true,
  67. EventHandler? onReceiveNotification,
  68. EventHandler? onOpenNotification,
  69. EventHandler? onReceiveMessage,
  70. }) {
  71. // print('---------------------------------------------------------------------push in jpush class------------------------------');
  72. // if (_inited) {
  73. // debugPrint('重复注册JpushUtil,此次注册无效,请忽略');
  74. // return;
  75. // }
  76. // bool _isReadPrivacy = WisStorageUtil.get(key: 'isReadPrivacy') ?? false;
  77. // print(_isReadPrivacy);
  78. // print('---------------------------------------------------------------------isReadPrivacy--------------------------------');
  79. // // 检查用户是否同意隐私政策
  80. // if (!_isReadPrivacy) {
  81. // _jpush.setAuth(enable: false);
  82. // debugPrint('---------------------------------------------------------------------用户未同意隐私政策,极光推送初始化已跳过--------------------------------------------------------------------');
  83. // return;
  84. // }
  85. // _jpush = JPush();
  86. // _inited = true;
  87. // _jpush.addEventHandler(
  88. // onReceiveNotification: onReceiveNotification,
  89. // onOpenNotification: onOpenNotification,
  90. // onReceiveMessage: onReceiveMessage,
  91. // );
  92. // _jpush.setup(
  93. // appKey: appKey,
  94. // channel: channel ?? 'theChannel',
  95. // production: production,
  96. // debug: false,
  97. // );
  98. // // 允许极光收集
  99. // _jpush.setAuth(enable: true);
  100. // ///申请推送权限,注意这个方法只会向用户弹出一次推送权限请求,
  101. // ///该方法还配置IOS角标、是否有声音的工作等
  102. // ///如果用户不同意,之后只能用户到设置页面里面勾选相应权限,
  103. // ///配置IOS角标、是否有声音的工作
  104. // if (Platform.isIOS) {
  105. // _jpush.applyPushAuthority(
  106. // NotificationSettingsIOS(sound: true, alert: true, badge: true),
  107. // );
  108. // }
  109. ///Android权限申请
  110. // if (Platform.isAndroid){
  111. // WPermissionUtil.request(
  112. // service: Permission.notification,
  113. // deniedText: '无法获取您的通知权限,是否前往设置?',
  114. // guidance: true,
  115. // );
  116. // }
  117. }
  118. _check() {
  119. assert(_inited, '请先在main函数中调用WJpushUtil.setup初始化极光推送');
  120. }
  121. ///获取 registrationId,这个 JPush 运行通过 registrationId 来进行推送.
  122. Future<String> get getRegistrationID {
  123. _check();
  124. return _jpush.getRegistrationID();
  125. }
  126. ///停止推送功能,调用该方法将不会接收到通知。
  127. Future<void> stopPush() {
  128. _check();
  129. return _jpush.stopPush();
  130. }
  131. ///调用 stopPush 后,可以通过 resumePush 方法恢复推送。
  132. Future resumePush() {
  133. _check();
  134. return _jpush.resumePush();
  135. }
  136. ///设置别名,极光后台可以通过别名来推送,一个 App 应用只有一个别名,一般用来存储用户 id。
  137. Future<Map<dynamic, dynamic>> setAlias(String alias) {
  138. _check();
  139. return _jpush.setAlias(alias);
  140. }
  141. ///可以通过 deleteAlias 方法来删除已经设置的 alias。
  142. Future<Map<dynamic, dynamic>> deleteAlias() {
  143. _check();
  144. return _jpush.deleteAlias();
  145. }
  146. ///在原来的 Tags 列表上添加指定 tags。
  147. Future<Map<dynamic, dynamic>> addTags(List<String> tags) {
  148. _check();
  149. return _jpush.addTags(tags);
  150. }
  151. ///在原来的 Tags 列表上删除指定 tags。
  152. Future<Map<dynamic, dynamic>> deleteTags(List<String> tags) {
  153. _check();
  154. return _jpush.deleteTags(tags);
  155. }
  156. ///重置 tags。
  157. Future<Map<dynamic, dynamic>> setTags(List<String> tags) {
  158. _check();
  159. return _jpush.setTags(tags);
  160. }
  161. ///清空所有 tags
  162. Future<Map<dynamic, dynamic>> cleanTags() {
  163. _check();
  164. return _jpush.cleanTags();
  165. }
  166. ///获取当前 tags 列表。
  167. Future<Map<dynamic, dynamic>> get getAllTags {
  168. _check();
  169. return _jpush.getAllTags();
  170. }
  171. ///指定触发时间,添加本地推送通知。
  172. // Future<String> sendLocalNotification(LocalNotification notification) {
  173. // _check();
  174. // return _jpush.sendLocalNotification(notification);
  175. // }
  176. ///清除通知栏上所有通知。
  177. Future<void> clearAllNotifications() {
  178. _check();
  179. return _jpush.clearAllNotifications();
  180. }
  181. ///清空通知栏上某个通知
  182. void clearNotification(int notificationId) {
  183. _check();
  184. return _jpush.clearNotification(notificationId: notificationId);
  185. }
  186. ///设置应用 Badge(小红点)
  187. ///IOS/华为Android
  188. Future<void> setBadge(int badge) {
  189. _check();
  190. return _jpush.setBadge(badge);
  191. }
  192. /// iOS Only
  193. /// 点击推送启动应用的时候原生会将该 notification 缓存起来,该方法用于获取缓存 notification
  194. /// 注意:notification 可能是 remoteNotification 和 localNotification,两种推送字段不一样。
  195. /// 如果不是通过点击推送启动应用,比如点击应用 icon 直接启动应用,notification 会返回 @{}。
  196. /// @param {Function} callback = (Object) => {}
  197. ///
  198. Future<Map<dynamic, dynamic>> getLaunchAppNotification() async {
  199. _check();
  200. return _jpush.getLaunchAppNotification();
  201. }
  202. }