Skip to content

Commit c7900be

Browse files
author
menelike
committed
be able to clear notifications: data: {clearNotifications: [int notId]}
props to phonegap#2619
1 parent 789e1ae commit c7900be

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

src/android/com/adobe/phonegap/push/FCMService.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,28 @@ public void onMessageReceived(RemoteMessage message) {
9898
String titleKey = prefs.getString(TITLE_KEY, TITLE);
9999

100100
extras = normalizeExtras(applicationContext, extras, messageKey, titleKey);
101+
String clearNotifications = extras.getString(CLEAR_NOTIFICATIONS);
101102

102103
if (clearBadge) {
103104
PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0);
104105
}
105106

107+
if (clearNotifications != null) {
108+
Log.d(LOG_TAG, clearNotifications);
109+
try {
110+
Log.d(LOG_TAG, "cancel notifications " + clearNotifications);
111+
JSONArray notificationIds = new JSONArray(clearNotifications);
112+
if (notificationIds.length() != 0) {
113+
for (int i = 0; i < notificationIds.length(); i++) {
114+
int clearNotificationId = notificationIds.getInt(i);
115+
PushPlugin.clearNotification(getApplicationContext(), clearNotificationId);
116+
}
117+
}
118+
} catch (JSONException e) {
119+
Log.e(LOG_TAG, "malformed clear notifications =[" + clearNotifications + "]");
120+
}
121+
}
122+
106123
// if we are in the foreground and forceShow is `false` only send data
107124
if (!forceShow && PushPlugin.isInForeground()) {
108125
Log.d(LOG_TAG, "foreground");
@@ -128,6 +145,24 @@ else if (forceShow && PushPlugin.isInForeground()) {
128145
}
129146
}
130147
}
148+
/*
149+
* Cancel a notification
150+
*/
151+
private void cancelNotification(int notificationId) {
152+
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
153+
String appName = getAppName(this);
154+
155+
if (notificationId != 0) {
156+
Log.d(LOG_TAG, "cancel notification id: " + notificationId);
157+
setNotification(notificationId, "");
158+
try {
159+
notificationManager.cancel(appName, notificationId);
160+
} catch (NullPointerException e) {
161+
Log.e(LOG_TAG, "could not cancel notification id: " + notificationId);
162+
}
163+
}
164+
}
165+
131166

132167
/*
133168
* Change a values key in the extras bundle

src/android/com/adobe/phonegap/push/PushPlugin.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,13 @@ private void clearAllNotifications() {
534534
}
535535

536536
private void clearNotification(int id) {
537-
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
538-
.getSystemService(Context.NOTIFICATION_SERVICE);
539-
String appName = (String) this.cordova.getActivity().getPackageManager()
540-
.getApplicationLabel(this.cordova.getActivity().getApplicationInfo());
537+
PushPlugin.clearNotification(cordova.getActivity(), id);
538+
}
539+
540+
static void clearNotification(Context context, int id) {
541+
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
542+
String appName = (String) context.getPackageManager()
543+
.getApplicationLabel(context.getApplicationInfo());
541544
notificationManager.cancel(appName, id);
542545
}
543546

src/ios/AppDelegate+notification.m

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
9696
silent = [contentAvailable integerValue];
9797
}
9898

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

132135
} else {
136+
[self clearNotifications:userInfo];
133137
completionHandler(UIBackgroundFetchResultNoData);
134138
}
135139
}
@@ -186,6 +190,33 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification {
186190
[[NSNotificationCenter defaultCenter] postNotificationName:pushPluginApplicationDidBecomeActiveNotification object:nil];
187191
}
188192

193+
- (void)clearNotifications:(NSDictionary *)userInfo
194+
{
195+
PushPlugin *pushHandler = [self getCommandInstance:@"PushNotification"];
196+
197+
@try {
198+
NSString * clearNotificationsString = [userInfo objectForKey:@"clearNotifications"];
199+
if (clearNotificationsString != nil) {
200+
clearNotificationsString = [clearNotificationsString stringByReplacingOccurrencesOfString:@"[" withString:@""];
201+
clearNotificationsString = [clearNotificationsString stringByReplacingOccurrencesOfString:@"]" withString:@""];
202+
203+
NSArray * clearNotificationsArray = [clearNotificationsString componentsSeparatedByString:@","];
204+
205+
for (NSString *notId in clearNotificationsArray){
206+
if ([notId isKindOfClass:[NSString class]]) {
207+
NSNumber *clearNotificationId = @([notId integerValue]);
208+
if (clearNotificationId > 0) {
209+
NSLog(@"Push Plugin clearing notId %@", clearNotificationId);
210+
[pushHandler clearRealNotification:clearNotificationId];
211+
}
212+
}
213+
}
214+
}
215+
} @catch (NSException *exception) {
216+
NSLog(@"Push Plugin could not parse clearNotifications array");
217+
}
218+
}
219+
189220
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
190221
willPresentNotification:(UNNotification *)notification
191222
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
@@ -197,7 +228,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
197228
pushHandler.isInline = YES;
198229
[pushHandler notificationReceived];
199230

200-
completionHandler(UNNotificationPresentationOptionNone);
231+
completionHandler(UNNotificationPresentationOptionBadge);
201232
}
202233

203234
- (void)userNotificationCenter:(UNUserNotificationCenter *)center

src/ios/PushPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- (void)subscribe:(CDVInvokedUrlCommand*)command;
6161
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
6262
- (void)clearNotification:(CDVInvokedUrlCommand*)command;
63+
- (void)clearRealNotification:(NSNumber*)notId;
6364

6465
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
6566
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

src/ios/PushPlugin.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,8 @@ - (void)notificationReceived {
466466
}
467467
}
468468

469-
- (void)clearNotification:(CDVInvokedUrlCommand *)command
469+
- (void)clearRealNotification:(NSNumber *)notId
470470
{
471-
NSNumber *notId = [command.arguments objectAtIndex:0];
472471
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
473472
/*
474473
* 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
481480
[matchingNotificationIdentifiers addObject:notification.request.identifier];
482481
}
483482
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:matchingNotificationIdentifiers];
484-
485-
NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId];
486-
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
487-
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
488483
}];
489484
}
490485

486+
- (void)clearNotification:(CDVInvokedUrlCommand *)command
487+
{
488+
NSNumber *notId = [command.arguments objectAtIndex:0];
489+
[self clearRealNotification: notId];
490+
491+
NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId];
492+
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
493+
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
494+
}
495+
491496
- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command
492497
{
493498
NSMutableDictionary* options = [command.arguments objectAtIndex:0];

0 commit comments

Comments
 (0)