jpush_helper.dart 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'package:flutter/services.dart';
  2. import 'package:jpush_flutter/jpush_flutter.dart';
  3. import 'package:jpush_flutter/jpush_interface.dart';
  4. import 'package:location/data/consts/build_config.dart';
  5. import 'package:location/utils/atmob_log.dart';
  6. class JpushHelper {
  7. static const String tag = 'JpushHelper';
  8. JpushHelper._();
  9. static JPushFlutterInterface? jpush;
  10. static void init() async {
  11. jpush = JPush.newJPush();
  12. _setJPushEventHandler();
  13. jpush!.setAuth(enable: true);
  14. jpush!.setup(
  15. appKey: "cd602bd396037438bb9ead5a",
  16. debug: BuildConfig.isDebug,
  17. );
  18. final id = await JpushHelper.getRegistrationId();
  19. AtmobLog.d(tag, 'RegistrationId:$id');
  20. }
  21. static Future<String> getRegistrationId() {
  22. if (jpush == null) {
  23. throw Exception('JPush not initialized');
  24. }
  25. return jpush!.getRegistrationID();
  26. }
  27. static void _setJPushEventHandler() {
  28. try {
  29. if (jpush == null) {
  30. throw Exception('JPush not initialized');
  31. }
  32. jpush!.addEventHandler(
  33. onReceiveNotification: (Map<String, dynamic> message) async {
  34. print("flutter onReceiveNotification: $message");
  35. }, onOpenNotification: (Map<String, dynamic> message) async {
  36. print("flutter onOpenNotification: $message");
  37. }, onReceiveMessage: (Map<String, dynamic> message) async {
  38. print("flutter onReceiveMessage: $message");
  39. }, onReceiveNotificationAuthorization:
  40. (Map<String, dynamic> message) async {
  41. print("flutter onReceiveNotificationAuthorization: $message");
  42. }, onNotifyMessageUnShow: (Map<String, dynamic> message) async {
  43. print("flutter onNotifyMessageUnShow: $message");
  44. }, onInAppMessageShow: (Map<String, dynamic> message) async {
  45. print("flutter onInAppMessageShow: $message");
  46. }, onCommandResult: (Map<String, dynamic> message) async {
  47. print("flutter onCommandResult: $message");
  48. }, onInAppMessageClick: (Map<String, dynamic> message) async {
  49. print("flutter onInAppMessageClick: $message");
  50. }, onConnected: (Map<String, dynamic> message) async {
  51. print("flutter onConnected: $message");
  52. }, onReceiveDeviceToken: (Map<String, dynamic> message) async {
  53. print("flutter onReceiveDeviceToken: $message");
  54. });
  55. } on PlatformException {
  56. AtmobLog.e(tag,
  57. 'JPush event handler error: JPush not initialized or platform error');
  58. } catch (e) {
  59. AtmobLog.e(tag, 'JPush event handler error: $e');
  60. }
  61. }
  62. }