import 'package:flutter/services.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; import 'package:jpush_flutter/jpush_interface.dart'; import 'package:location/data/consts/build_config.dart'; import 'package:location/utils/atmob_log.dart'; class JpushHelper { static const String tag = 'JpushHelper'; JpushHelper._(); static JPushFlutterInterface? jpush; static void init() async { jpush = JPush.newJPush(); _setJPushEventHandler(); jpush!.setAuth(enable: true); jpush!.setup( appKey: "cd602bd396037438bb9ead5a", debug: BuildConfig.isDebug, ); final id = await JpushHelper.getRegistrationId(); AtmobLog.d(tag, 'RegistrationId:$id'); } static Future getRegistrationId() { if (jpush == null) { throw Exception('JPush not initialized'); } return jpush!.getRegistrationID(); } static void _setJPushEventHandler() { try { if (jpush == null) { throw Exception('JPush not initialized'); } jpush!.addEventHandler( onReceiveNotification: (Map message) async { print("flutter onReceiveNotification: $message"); }, onOpenNotification: (Map message) async { print("flutter onOpenNotification: $message"); }, onReceiveMessage: (Map message) async { print("flutter onReceiveMessage: $message"); }, onReceiveNotificationAuthorization: (Map message) async { print("flutter onReceiveNotificationAuthorization: $message"); }, onNotifyMessageUnShow: (Map message) async { print("flutter onNotifyMessageUnShow: $message"); }, onInAppMessageShow: (Map message) async { print("flutter onInAppMessageShow: $message"); }, onCommandResult: (Map message) async { print("flutter onCommandResult: $message"); }, onInAppMessageClick: (Map message) async { print("flutter onInAppMessageClick: $message"); }, onConnected: (Map message) async { print("flutter onConnected: $message"); }, onReceiveDeviceToken: (Map message) async { print("flutter onReceiveDeviceToken: $message"); }); } on PlatformException { AtmobLog.e(tag, 'JPush event handler error: JPush not initialized or platform error'); } catch (e) { AtmobLog.e(tag, 'JPush event handler error: $e'); } } }