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/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 0290c8ecc..01e444ea5 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -466,9 +466,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 +480,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]; + [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 { NSMutableDictionary* options = [command.arguments objectAtIndex:0];