From 9c7173f9fdd9e25f2ea639fc61ea31708edfe292 Mon Sep 17 00:00:00 2001 From: Jochum van der Ploeg Date: Thu, 1 Nov 2018 16:47:55 +0100 Subject: [PATCH 1/2] Initial setup --- .../com/adobe/phonegap/push/FCMService.java | 21 ++++++++++++++++++- .../com/adobe/phonegap/push/PushPlugin.java | 11 ++++++---- src/ios/PushPlugin.m | 16 +++++++++++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/android/com/adobe/phonegap/push/FCMService.java b/src/android/com/adobe/phonegap/push/FCMService.java index db1738a72..58b210168 100644 --- a/src/android/com/adobe/phonegap/push/FCMService.java +++ b/src/android/com/adobe/phonegap/push/FCMService.java @@ -103,8 +103,13 @@ public void onMessageReceived(RemoteMessage message) { PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0); } + int clearNotificationId = getClearNotificationId(extras); + + // if the notification has a `clearNotification`, then clear it. + if(clearNotificationId != -1) { + PushPlugin.clearNotification(getApplicationContext(), clearNotificationId); // if we are in the foreground and forceShow is `false` only send data - if (!forceShow && PushPlugin.isInForeground()) { + } else if (!forceShow && PushPlugin.isInForeground()) { Log.d(LOG_TAG, "foreground"); extras.putBoolean(FOREGROUND, true); extras.putBoolean(COLDSTART, false); @@ -302,6 +307,20 @@ private Bundle normalizeExtras(Context context, Bundle extras, String messageKey return newExtras; } + private int getClearNotificationId(Bundle extras) { + int clearNotificationId = -1; + String msgcnid = extras.getString(CLEAR_NOTIFICATION); + + try { + if (msgcnid != null) { + clearNotificationId = Integer.parseInt(msgcnid); + } + } catch (NumberFormatException e) { + Log.e(LOG_TAG, e.getLocalizedMessage(), e); + } + return clearNotificationId; + } + private int extractBadgeCount(Bundle extras) { int count = -1; String msgcnt = extras.getString(COUNT); diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 690b37776..bc32e308b 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -534,10 +534,13 @@ private void clearAllNotifications() { } private void clearNotification(int id) { - final NotificationManager notificationManager = (NotificationManager) cordova.getActivity() - .getSystemService(Context.NOTIFICATION_SERVICE); - String appName = (String) this.cordova.getActivity().getPackageManager() - .getApplicationLabel(this.cordova.getActivity().getApplicationInfo()); + PushPlugin.clearNotification(cordova.getActivity(), id); + } + + public static void clearNotification(Context context, int id) { + final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + String appName = (String) context.getPackageManager() + .getApplicationLabel(context.getApplicationInfo()); notificationManager.cancel(appName, id); } diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 0290c8ecc..c0ac9c7a3 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -442,6 +442,11 @@ - (void)notificationReceived { } } + if([additionalData objectForKey:@"clearNotification") { + NSNumber *clearNotificationId = @([[additionalData objectForKey:@"clearNotification"] integerValue]); + [clearRealNotification: clearNotificationId]; + } + if (isInline) { [additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"foreground"]; } else { @@ -466,9 +471,8 @@ - (void)notificationReceived { } } -- (void)clearNotification:(CDVInvokedUrlCommand *)command +- (void)clearRealNotification:(NSNumber *)notId { - NSNumber *notId = [command.arguments objectAtIndex:0]; [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray * _Nonnull notifications) { /* * If the server generates a unique "notId" for every push notification, there should only be one match in these arrays, but if not, it will delete @@ -481,13 +485,19 @@ - (void)clearNotification:(CDVInvokedUrlCommand *)command [matchingNotificationIdentifiers addObject:notification.request.identifier]; } [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:matchingNotificationIdentifiers]; - + NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId]; CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; }]; } +- (void)clearNotification:(CDVInvokedUrlCommand *)command +{ + NSNumber *notId = [command.arguments objectAtIndex:0]; + [clearRealNotification: notId]; +} + - (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { NSMutableDictionary* options = [command.arguments objectAtIndex:0]; From a614c418afc365922231527846d1886388fa1fa9 Mon Sep 17 00:00:00 2001 From: Jochum van der Ploeg Date: Wed, 7 Nov 2018 15:22:04 +0100 Subject: [PATCH 2/2] iOS update --- src/ios/AppDelegate+notification.m | 10 ++++++++-- src/ios/PushPlugin.h | 2 ++ src/ios/PushPlugin.m | 15 +++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ios/AppDelegate+notification.m b/src/ios/AppDelegate+notification.m index b04c47687..98128f13c 100644 --- a/src/ios/AppDelegate+notification.m +++ b/src/ios/AppDelegate+notification.m @@ -83,7 +83,6 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N // app is in the background or inactive, so only call notification callback if this is a silent push if (application.applicationState != UIApplicationStateActive) { - NSLog(@"app in-active"); // do some convoluted logic to find out if this should be a silent push. @@ -110,6 +109,13 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N pushHandler.handlerObj = [NSMutableDictionary dictionaryWithCapacity:2]; } + // Check if this is a clear notification. + id clearNotification = [userInfo objectForKey:@"clearNotification"]; + if ([clearNotification isKindOfClass:[NSString class]]) { + NSNumber *clearNotificationId = @([clearNotification integerValue]); + [pushHandler clearRealNotification: clearNotificationId]; + } + id notId = [userInfo objectForKey:@"notId"]; if (notId != nil) { NSLog(@"Push Plugin notId %@", notId); @@ -154,7 +160,7 @@ - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull voi - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { NSLog(@"active"); - + NSString *firstLaunchKey = @"firstLaunchKey"; NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"phonegap-plugin-push"]; if (![defaults boolForKey:firstLaunchKey]) { diff --git a/src/ios/PushPlugin.h b/src/ios/PushPlugin.h index 4b6ec930a..40b425d7b 100644 --- a/src/ios/PushPlugin.h +++ b/src/ios/PushPlugin.h @@ -61,6 +61,8 @@ - (void)unsubscribe:(CDVInvokedUrlCommand*)command; - (void)clearNotification:(CDVInvokedUrlCommand*)command; +- (void)clearRealNotification:(NSNumber*)notId; + - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index c0ac9c7a3..01e444ea5 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -442,11 +442,6 @@ - (void)notificationReceived { } } - if([additionalData objectForKey:@"clearNotification") { - NSNumber *clearNotificationId = @([[additionalData objectForKey:@"clearNotification"] integerValue]); - [clearRealNotification: clearNotificationId]; - } - if (isInline) { [additionalData setObject:[NSNumber numberWithBool:YES] forKey:@"foreground"]; } else { @@ -485,17 +480,17 @@ - (void)clearRealNotification:(NSNumber *)notId [matchingNotificationIdentifiers addObject:notification.request.identifier]; } [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:matchingNotificationIdentifiers]; - - NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId]; - CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; - [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; }]; } - (void)clearNotification:(CDVInvokedUrlCommand *)command { NSNumber *notId = [command.arguments objectAtIndex:0]; - [clearRealNotification: notId]; + [self clearRealNotification: notId]; + + NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId]; + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; + [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; } - (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command