import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; import 'package:star_lock/flavors.dart'; import 'package:star_lock/mine/minePersonInfo/minePersonInfoEditAccount/minePersonInfoEditAccount/mineUnbindPhoneOrEmail_entity.dart'; import 'package:star_lock/network/api_repository.dart'; import 'package:star_lock/tools/baseGetXController.dart'; import 'package:star_lock/tools/push/message_management.dart'; import 'package:star_lock/tools/push/notification_service.dart'; import 'package:star_lock/tools/storage.dart'; import '../../app_settings/app_settings.dart'; class XSJPushProvider { static const Map channelTypeMapping = { 1: 'xiaomi', 2: 'huawei', 3: 'meizu', 4: 'oppo', 5: 'vivo', 6: 'honor', 7: 'apns', 8: 'fcm', 9: 'jiguang' }; final JPush jpush = JPush(); Completer>? _jpushRegistrationIdCompleter; Completer>? _vendorTokenCompleter; Future resetJPushService() async { debugPrint("resetJPushService start"); jpush.setup( appKey: '', channel: 'flutter_channel', production: F.isProductionEnv, debug: !F.isProductionEnv, ); } // appKey: 251fc8074820d122b6de58d2--鑫泓佳AppKey // appKey: 7ff37d174c1a568a89e98dad--sky Future initJPushService() async { debugPrint("initJPushService start"); _jpushRegistrationIdCompleter = Completer>(); _vendorTokenCompleter = Completer>(); final String? data = await Storage.getString(saveUserLoginData); if (data == null || data.isEmpty) { AppLog.log('No user data found.'); return; } AppLog.log('jPushKey ${F.jPushKey}'); // final String? bundleIdentifier = // await NativeInteractionTool().getBundleIdentifier(); // print('bundleIdentifier: $bundleIdentifier'); jpush.setup( appKey: F.jPushKey, channel: 'flutter_channel', production: F.isProductionEnv, debug: !F.isProductionEnv, ); jpush.applyPushAuthority( const NotificationSettingsIOS(sound: true, alert: true, badge: false), ); addJPushEventHandler(); AppLog.log('JPush initialized.'); debugPrint("initJPushService end"); } static const int CMD_GET_REGISTRATION_ID = 2005; //getRegistrationID 异步回调 static const int CMD_GET_TOKEN = 10000; //厂商 token 注册回调 //极光推送事件处理方法 void addJPushEventHandler() { jpush.addEventHandler( onCommandResult: (Map data) async { AppLog.log('onCommandResult: $data'); debugPrint("addJPushEventHandler onCommandResult:$data"); final int cmdCode = data['cmd']; switch (cmdCode) { case CMD_GET_REGISTRATION_ID: final bool isNullOrBlank = GetUtils.isNullOrBlank(data['message']) ?? true; if (!(_jpushRegistrationIdCompleter?.isCompleted ?? true) && !isNullOrBlank) { await Storage.setString(pushDeviceID, data['message']); AppLog.log('flutter get registration id : ${data['message']}'); _jpushRegistrationIdCompleter?.complete({ 'channel': 'jiguang', 'channelToken': data['message'] }); final String? channel2TokenStr = await Storage.getString(vendorPushChannelInfo); if (Platform.isAndroid && !(_vendorTokenCompleter?.isCompleted ?? true) && channel2TokenStr != null) { _vendorTokenCompleter?.complete(jsonDecode(channel2TokenStr)); } } break; case CMD_GET_TOKEN: final bool isNullOrBlank = GetUtils.isNullOrBlank(data['token']) ?? true; if (!(_vendorTokenCompleter?.isCompleted ?? true) && !isNullOrBlank) { final Map channel2Token = { 'channel': channelTypeMapping[data['platform']], 'channelToken': data['token'] }; await Storage.setString( vendorPushChannelInfo, jsonEncode(channel2Token)); _vendorTokenCompleter?.complete(channel2Token); } break; } }, onReceiveNotification: (Map message) async { AppLog.log('onReceiveNotification: $message'); debugPrint('addJPushEventHandler onReceiveNotification:$message'); // showCustomNotification(message); }, onOpenNotification: (Map message) async { AppLog.log('onOpenNotification: $message'); debugPrint("addJPushEventHandler onOpenNotification:$message"); }, onReceiveMessage: (Map message) async { AppLog.log('onReceiveMessage: $message'); debugPrint("addJPushEventHandler onReceiveMessage:$message"); //这里接收自定义消息 MessageManagement.shunting(message); }, onReceiveNotificationAuthorization: (Map message) async { AppLog.log('onReceiveNotificationAuthorization: $message'); debugPrint( "addJPushEventHandler onReceiveNotificationAuthorization:$message"); }, onInAppMessageShow: (Map message) async { AppLog.log('onInAppMessageShow: $message'); debugPrint("addJPushEventHandler onInAppMessageShow:$message"); }, onConnected: (Map message) async { return Future.value(); }, ); bindPushChannels(); } // jpush 统一推送通道设备绑定 Future bindPushChannels() async { try { if (_jpushRegistrationIdCompleter == null || _vendorTokenCompleter == null) { return; } debugPrint("await PushChannels start"); final List> channels = await Future.wait([ _jpushRegistrationIdCompleter!.future, _vendorTokenCompleter!.future ]); final String? registrationId = await Storage.getString(pushDeviceID); debugPrint("await PushChannels end"); final MineUnbindPhoneOrEmailEntity entity = await ApiRepository.to.pushBindChannels(registrationId!, channels); if (entity.errorCode!.codeIsSuccessful) { AppLog.log('绑定成功'); } else { AppLog.log('绑定失败'); } } catch (e) { AppLog.log('Error binding device ID: $e'); } } void showCustomNotification(Map data) { final String title = data['notification']['android']['title'] ?? '默认标题'; final String content = data['notification']['android']['alert'] ?? '默认内容'; final String imageUrl = data['notification']['android']['extras'] ?['image_url']; // 从 extras 获取图片 NotificationService().showImageNotification(title, content, imageUrl); } }