Skip to content

Commit

Permalink
be able to clear notifications: data: {clearNotifications: [int notId]}
Browse files Browse the repository at this point in the history
props to phonegap#2619
  • Loading branch information
menelike committed May 24, 2019
1 parent 789e1ae commit c7900be
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
35 changes: 35 additions & 0 deletions src/android/com/adobe/phonegap/push/FCMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,28 @@ public void onMessageReceived(RemoteMessage message) {
String titleKey = prefs.getString(TITLE_KEY, TITLE);

extras = normalizeExtras(applicationContext, extras, messageKey, titleKey);
String clearNotifications = extras.getString(CLEAR_NOTIFICATIONS);

if (clearBadge) {
PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0);
}

if (clearNotifications != null) {
Log.d(LOG_TAG, clearNotifications);
try {
Log.d(LOG_TAG, "cancel notifications " + clearNotifications);
JSONArray notificationIds = new JSONArray(clearNotifications);
if (notificationIds.length() != 0) {
for (int i = 0; i < notificationIds.length(); i++) {
int clearNotificationId = notificationIds.getInt(i);
PushPlugin.clearNotification(getApplicationContext(), clearNotificationId);
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, "malformed clear notifications =[" + clearNotifications + "]");
}
}

// if we are in the foreground and forceShow is `false` only send data
if (!forceShow && PushPlugin.isInForeground()) {
Log.d(LOG_TAG, "foreground");
Expand All @@ -128,6 +145,24 @@ else if (forceShow && PushPlugin.isInForeground()) {
}
}
}
/*
* Cancel a notification
*/
private void cancelNotification(int notificationId) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String appName = getAppName(this);

if (notificationId != 0) {
Log.d(LOG_TAG, "cancel notification id: " + notificationId);
setNotification(notificationId, "");
try {
notificationManager.cancel(appName, notificationId);
} catch (NullPointerException e) {
Log.e(LOG_TAG, "could not cancel notification id: " + notificationId);
}
}
}


/*
* Change a values key in the extras bundle
Expand Down
11 changes: 7 additions & 4 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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);
}

Expand Down
35 changes: 33 additions & 2 deletions src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
silent = [contentAvailable integerValue];
}

if (silent == 1) {
NSString * clearNotificationsString = [userInfo objectForKey:@"clearNotifications"];
// don't wakeup the app on clearNotifications - this is a decision based on performance
if (silent == 1 && clearNotificationsString == nil) {
NSLog(@"this should be a silent push");
void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -123,13 +125,15 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
pushHandler.isInline = NO;
[pushHandler notificationReceived];
} else {
[self clearNotifications:userInfo];
NSLog(@"just put it in the shade");
//save it for later
self.launchNotification = userInfo;
completionHandler(UIBackgroundFetchResultNewData);
}

} else {
[self clearNotifications:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}
}
Expand Down Expand Up @@ -186,6 +190,33 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:pushPluginApplicationDidBecomeActiveNotification object:nil];
}

- (void)clearNotifications:(NSDictionary *)userInfo
{
PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];

@try {
NSString * clearNotificationsString = [userInfo objectForKey:@"clearNotifications"];
if (clearNotificationsString != nil) {
clearNotificationsString = [clearNotificationsString stringByReplacingOccurrencesOfString:@"[" withString:@""];
clearNotificationsString = [clearNotificationsString stringByReplacingOccurrencesOfString:@"]" withString:@""];

NSArray * clearNotificationsArray = [clearNotificationsString componentsSeparatedByString:@","];

for (NSString *notId in clearNotificationsArray){
if ([notId isKindOfClass:[NSString class]]) {
NSNumber *clearNotificationId = @([notId integerValue]);
if (clearNotificationId > 0) {
NSLog(@"Push Plugin clearing notId %@", clearNotificationId);
[pushHandler clearRealNotification:clearNotificationId];
}
}
}
}
} @catch (NSException *exception) {
NSLog(@"Push Plugin could not parse clearNotifications array");
}
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
Expand All @@ -197,7 +228,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
pushHandler.isInline = YES;
[pushHandler notificationReceived];

completionHandler(UNNotificationPresentationOptionNone);
completionHandler(UNNotificationPresentationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
Expand Down
1 change: 1 addition & 0 deletions src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- (void)subscribe:(CDVInvokedUrlCommand*)command;
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
- (void)clearNotification:(CDVInvokedUrlCommand*)command;
- (void)clearRealNotification:(NSNumber*)notId;

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
Expand Down
17 changes: 11 additions & 6 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,8 @@ - (void)notificationReceived {
}
}

- (void)clearNotification:(CDVInvokedUrlCommand *)command
- (void)clearRealNotification:(NSNumber *)notId
{
NSNumber *notId = [command.arguments objectAtIndex:0];
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _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
Expand All @@ -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];
Expand Down

0 comments on commit c7900be

Please sign in to comment.