From c80f769970fd9ed7e812c2c8f44f9222c9d286ca Mon Sep 17 00:00:00 2001 From: huangminlinux <380108184@qq.com> Date: Tue, 29 Jan 2019 17:26:12 +0800 Subject: [PATCH] update fix demo to link native plugin --- CHANGELOG.md | 2 +- README.md | 2 +- example/lib/main.dart | 32 +++++++++--------- example/pubspec.lock | 8 ++--- example/pubspec.yaml | 4 +-- ios/Classes/JPushPlugin.m | 68 +++++++++++++++++++++------------------ pubspec.yaml | 2 +- 7 files changed, 62 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbe1282..1c5b4a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.0.10 +## 0.0.11 iOS: 修复 getLaunchAppNotification 返回 null 的情况。 featurn: APNS 推送字段将 extras 字段移动到 notification.extras 中和 android 保持一致。 diff --git a/README.md b/README.md index 0d0cc4d..b1dea86 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ```yaml dependencies: - jpush_flutter: 0.0.10 + jpush_flutter: 0.0.11 ``` ### 配置 diff --git a/example/lib/main.dart b/example/lib/main.dart index d8bd95b..0a7ed6b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -116,7 +116,22 @@ final JPush jpush = new JPush(); }); }), + new FlatButton( + child: new Text('getLaunchAppNotification\n'), + onPressed: () { + + jpush.getLaunchAppNotification().then((map) { + setState(() { + debugLable = "getLaunchAppNotification success: $map"; + }); + }) + .catchError((error) { + setState(() { + debugLable = "getLaunchAppNotification error: $error"; + }); + }); + }), new FlatButton( child: new Text('applyPushAuthority\n'), onPressed: () { @@ -271,22 +286,7 @@ final JPush jpush = new JPush(); jpush.clearAllNotifications(); }), - new FlatButton( - child: new Text('getLaunchAppNotification\n'), - onPressed: () { - - jpush.getLaunchAppNotification().then((map) { - setState(() { - debugLable = "getLaunchAppNotification success: $map"; - }); - }) - .catchError((error) { - setState(() { - debugLable = "getLaunchAppNotification error: $error"; - }); - }); - - }), + ] ) diff --git a/example/pubspec.lock b/example/pubspec.lock index 0229094..c3fe1c0 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -133,10 +133,10 @@ packages: jpush_flutter: dependency: "direct dev" description: - name: jpush_flutter - url: "https://pub.dartlang.org" - source: hosted - version: "0.0.8" + path: ".." + relative: true + source: path + version: "0.0.11" js: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 0fc4fd1..47eb8e3 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -26,8 +26,8 @@ dev_dependencies: flutter_test: sdk: flutter - jpush_flutter: ^0.0.8 - # path: ../ + jpush_flutter: + path: ../ # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec diff --git a/ios/Classes/JPushPlugin.m b/ios/Classes/JPushPlugin.m index 5a6bd14..d74ed84 100644 --- a/ios/Classes/JPushPlugin.m +++ b/ios/Classes/JPushPlugin.m @@ -295,14 +295,7 @@ static NSMutableArray* getRidResults; } - (void)getLaunchAppNotification:(FlutterMethodCall*)call result:(FlutterResult)result { - NSDictionary *notification; - notification = [_launchNotification objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; - - if ([_launchNotification objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]) { - UILocalNotification *localNotification = [_launchNotification objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; - notification = localNotification.userInfo; - } - result(notification); + result(_launchNotification == nil ? @{}: _launchNotification); } - (void)getRegistrationID:(FlutterMethodCall*)call result:(FlutterResult)result { @@ -406,7 +399,22 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (launchOptions != nil) { _launchNotification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; + _launchNotification = [self jpushFormatAPNSDic:_launchNotification.copy]; + } + + if ([launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]) { + UILocalNotification *localNotification = [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; + NSMutableDictionary *localNotificationEvent = @{}.mutableCopy; + localNotificationEvent[@"content"] = localNotification.alertBody; + localNotificationEvent[@"badge"] = @(localNotification.applicationIconBadgeNumber); + localNotificationEvent[@"extras"] = localNotification.userInfo; + localNotificationEvent[@"fireTime"] = [NSNumber numberWithLong:[localNotification.fireDate timeIntervalSince1970] * 1000]; + localNotificationEvent[@"soundName"] = [localNotification.soundName isEqualToString:UILocalNotificationDefaultSoundName] ? @"" : localNotification.soundName; + if (@available(iOS 8.2, *)) { + localNotificationEvent[@"title"] = localNotification.alertTitle; + } + _launchNotification = localNotificationEvent; } return YES; } @@ -416,20 +424,6 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { } - (void)applicationDidBecomeActive:(UIApplication *)application { -// _resumingFromBackground = NO; - // Clears push notifications from the notification center, with the - // side effect of resetting the badge count. We need to clear notifications - // because otherwise the user could tap notifications in the notification - // center while the app is in the foreground, and we wouldn't be able to - // distinguish that case from the case where a message came in and the - // user dismissed the notification center without tapping anything. - // TODO(goderbauer): Revisit this behavior once we provide an API for managing - // the badge number, or if we add support for running Dart in the background. - // Setting badgeNumber to 0 is a no-op (= notifications will not be cleared) - // if it is already 0, - // therefore the next line is setting it to 1 first before clearing it again - // to remove all - // notifications. application.applicationIconBadgeNumber = 1; application.applicationIconBadgeNumber = 0; } @@ -437,7 +431,6 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - (bool)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { -// [self didReceiveRemoteNotification:userInfo]; [_channel invokeMethod:@"onReceiveNotification" arguments:userInfo]; completionHandler(UIBackgroundFetchResultNoData); @@ -461,28 +454,41 @@ didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSe -- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { - +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler API_AVAILABLE(ios(10.0)){ NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; - [_channel invokeMethod:@"onReceiveNotification" arguments: userInfo]; + [_channel invokeMethod:@"onReceiveNotification" arguments: [self jpushFormatAPNSDic:userInfo]]; } - completionHandler(notificationTypes); } -- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler API_AVAILABLE(ios(10.0)){ NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; -// [[NSNotificationCenter defaultCenter] postNotificationName:@"kJPFOpenNotification" object:userInfo]; - [_channel invokeMethod:@"onOpenNotification" arguments: userInfo]; - + [_channel invokeMethod:@"onOpenNotification" arguments: [self jpushFormatAPNSDic:userInfo]]; } completionHandler(); } +- (NSMutableDictionary *)jpushFormatAPNSDic:(NSDictionary *)dic { + NSMutableDictionary *extras = @{}.mutableCopy; + for (NSString *key in dic) { + if([key isEqualToString:@"_j_business"] || + [key isEqualToString:@"_j_msgid"] || + [key isEqualToString:@"_j_uid"] || + [key isEqualToString:@"actionIdentifier"] || + [key isEqualToString:@"aps"]) { + continue; + } + extras[key] = dic[key]; + } + NSMutableDictionary *formatDic = dic.mutableCopy; + formatDic[@"extras"] = extras; + return formatDic; +} + @end diff --git a/pubspec.yaml b/pubspec.yaml index 7807f84..8203ba7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: jpush_flutter description: Offically supported JPush Flutter plugin. -version: 0.0.10 +version: 0.0.11 author: huminios homepage: https://www.jiguang.cn