diff --git a/ios/Runner/AppDelegate.m b/ios/Runner/AppDelegate.m index c6d3389b..76d97654 100755 --- a/ios/Runner/AppDelegate.m +++ b/ios/Runner/AppDelegate.m @@ -9,6 +9,7 @@ @interface AppDelegate() +@property (nonatomic, strong) FlutterMethodChannel *methodChannel; @end @@ -20,11 +21,24 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // [UMConfigure initWithAppkey:@"671244ae80464b33f6df9646" channel:@"Product"]; + + + //Required + //notice: 3.0.0 及以后版本注册可以这样写,也可以继续用之前的注册方式 + JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; + entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings; + if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { + // 可以添加自定义 categories + // NSSet *categories for iOS10 or later + // NSSet *categories for iOS8 and iOS9 + } + [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; + XSFlutterManager *VC = [[XSFlutterManager alloc] init]; self.window.rootViewController = VC; [self.window makeKeyAndVisible]; - + return YES; } @@ -64,7 +78,7 @@ */ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - //sdk注册DeviceToken + /// Required - 注册 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; } @@ -72,7 +86,8 @@ * 苹果推送注册失败回调 */ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error); + //Optional + NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } /** @@ -96,6 +111,49 @@ NSLog(@"Receive message title: %@, content: %@.", title, body); } +# pragma mark- JPUSHRegisterDelegate + +// iOS 12 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{ + if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + //从通知界面直接进入应用 + }else{ + //从通知设置界面进入应用 + } +} + +// iOS 10 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { + // Required + NSDictionary * userInfo = notification.request.content.userInfo; + if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + [JPUSHService handleRemoteNotification:userInfo]; + } + completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置 +} + +// iOS 10 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { + // Required + NSDictionary * userInfo = response.notification.request.content.userInfo; + if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + [JPUSHService handleRemoteNotification:userInfo]; + } + completionHandler(); // 系统要求执行这个方法 +} + +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { + + // Required, iOS 7 Support + [JPUSHService handleRemoteNotification:userInfo]; + completionHandler(UIBackgroundFetchResultNewData); +} + +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { + + // Required, For systems with less than or equal to iOS 6 + [JPUSHService handleRemoteNotification:userInfo]; +} diff --git a/ios/Runner/XSFlutterManager.m b/ios/Runner/XSFlutterManager.m index d53ee425..c2ce6138 100755 --- a/ios/Runner/XSFlutterManager.m +++ b/ios/Runner/XSFlutterManager.m @@ -64,6 +64,7 @@ activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) { if (completed) { NSLog(@"completed"); + // 分享成功 } else { NSLog(@"canceled"); @@ -72,6 +73,10 @@ }; result(@"push返回到flutter"); } + if ([method isEqualToString:@"getBundleIdentifier"]) { + NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; + result(bundleIdentifier); // 返回 Bundle Identifier + } }]; [GeneratedPluginRegistrant registerWithRegistry:self]; diff --git a/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart b/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart index 9fb5ce97..f339dd9a 100644 --- a/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart +++ b/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart @@ -14,7 +14,6 @@ import 'package:star_lock/talk/starChart/proto/gateway_reset.pb.dart'; import 'package:star_lock/talk/starChart/proto/generic.pb.dart'; import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart'; import 'package:star_lock/talk/starChart/proto/talk_request.pb.dart'; -import 'package:star_lock/tools/push/xs_jPhush.dart'; import 'package:star_lock/tools/storage.dart'; class UdpTalkRequestHandler extends ScpMessageBaseHandle @@ -79,7 +78,9 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle // 收到来电请求时进行本地通知 Future _showTalkRequestNotification( {required String talkObjectName}) async { - final Map message = { + /** + * + final Map message = { 'platform': 'all', 'audience': 'all', 'notification': >{ @@ -95,8 +96,7 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle }; XSJPushProvider().showCustomNotification(message); - - /* + */ const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails('1', 'flutter_channel', importance: Importance.max, @@ -113,7 +113,6 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle await flutterLocalNotificationsPlugin.show(0, '呼叫提醒'.tr, '${'收到来自'.tr}($talkObjectName)${'锁的呼叫'.tr}。', platformChannelSpecifics, payload: 'item x'); - */ } @override diff --git a/lib/tools/NativeInteractionTool.dart b/lib/tools/NativeInteractionTool.dart index d7c0b943..5326d2bf 100755 --- a/lib/tools/NativeInteractionTool.dart +++ b/lib/tools/NativeInteractionTool.dart @@ -1,5 +1,6 @@ import 'package:flutter/services.dart'; import 'package:star_lock/flavors.dart'; +import 'package:star_lock/tools/push/xs_jPhush.dart'; import '../app_settings/app_settings.dart'; @@ -52,4 +53,15 @@ class NativeInteractionTool { } }); } + + Future getBundleIdentifier() async { + try { + final String? bundleIdentifier = + await sendChannel.invokeMethod('getBundleIdentifier'); + return bundleIdentifier; + } on PlatformException catch (e) { + print("Failed to get bundle identifier: '${e.message}'."); + return null; + } + } } diff --git a/lib/tools/push/xs_jPhush.dart b/lib/tools/push/xs_jPhush.dart index 4e25400d..d21cf996 100755 --- a/lib/tools/push/xs_jPhush.dart +++ b/lib/tools/push/xs_jPhush.dart @@ -1,11 +1,13 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.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/NativeInteractionTool.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'; @@ -15,7 +17,6 @@ import '../../app_settings/app_settings.dart'; class XSJPushProvider { final JPush jpush = JPush(); - final NotificationService _notificationService = NotificationService(); // appKey: 251fc8074820d122b6de58d2--鑫泓佳AppKey // appKey: 7ff37d174c1a568a89e98dad--sky @@ -27,6 +28,9 @@ class XSJPushProvider { } print('jPushKey ${F.jPushKey}'); + // final String? bundleIdentifier = + // await NativeInteractionTool().getBundleIdentifier(); + // print('bundleIdentifier: $bundleIdentifier'); jpush.setup( appKey: F.jPushKey, channel: 'flutter_channel', @@ -40,6 +44,10 @@ class XSJPushProvider { addJPushEventHandler(); AppLog.log('JPush initialized.'); + + final String rid = await jpush.getRegistrationID(); + print('flutter get registration id : $rid'); + pushBindDeviceID(rid); } //极光推送事件处理方法 @@ -69,11 +77,13 @@ class XSJPushProvider { final String rid = await jpush.getRegistrationID(); AppLog.log('onConnected registration id : $rid'); await Storage.setString(pushDeviceID, rid); - await pushBindDeviceID(rid, Platform.isAndroid ? 10 : 20); + await pushBindDeviceID(rid); return Future.value(); }, ); + + // Remove the incorrect addEventHandler call } Future showCustomNotification(Map message) async { @@ -86,7 +96,7 @@ class XSJPushProvider { NotificationService().showImageNotification(title, content, imageUrl); } - Future pushBindDeviceID(String deviceID, int deviceType) async { + Future pushBindDeviceID(String deviceID) async { try { if (deviceID.isEmpty) { AppLog.log('Device ID is empty.'); @@ -95,10 +105,9 @@ class XSJPushProvider { AppLog.log('onConnected registration id : $rid'); await Storage.setString(pushDeviceID, rid); deviceID = rid; - deviceType = Platform.isAndroid ? 10 : 20; } - final MineUnbindPhoneOrEmailEntity entity = - await ApiRepository.to.pushBindAppId(deviceID, deviceType); + final MineUnbindPhoneOrEmailEntity entity = await ApiRepository.to + .pushBindAppId(deviceID, Platform.isAndroid ? 10 : 20); if (entity.errorCode!.codeIsSuccessful) { AppLog.log('绑定成功'); } else {