Skip to content

Commit 0a65313

Browse files
author
menelike
committed
be able to clear notifications: data: {clearNotifications: [int notId]}
props to phonegap#2619
1 parent 82bbb1f commit 0a65313

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
@@ -99,11 +99,28 @@ public void onMessageReceived(RemoteMessage message) {
9999

100100
extras = normalizeExtras(applicationContext, extras, messageKey, titleKey);
101101
int contentUpdate = parseInt(CONTENT_UPDATE, extras);
102+
String clearNotifications = extras.getString(CLEAR_NOTIFICATIONS);
102103

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

108+
if (clearNotifications != null) {
109+
Log.d(LOG_TAG, clearNotifications);
110+
try {
111+
Log.d(LOG_TAG, "cancel notifications " + clearNotifications);
112+
JSONArray notificationIds = new JSONArray(clearNotifications);
113+
if (notificationIds.length() != 0) {
114+
for (int i = 0; i < notificationIds.length(); i++) {
115+
int clearNotificationId = notificationIds.getInt(i);
116+
PushPlugin.clearNotification(getApplicationContext(), clearNotificationId);
117+
}
118+
}
119+
} catch (JSONException e) {
120+
Log.e(LOG_TAG, "malformed clear notifications =[" + clearNotifications + "]");
121+
}
122+
}
123+
107124
if (contentUpdate == 1) {
108125
int notId = parseInt(NOT_ID, extras);
109126

@@ -139,6 +156,24 @@ else if (forceShow && PushPlugin.isInForeground()) {
139156
}
140157
}
141158
}
159+
/*
160+
* Cancel a notification
161+
*/
162+
private void cancelNotification(int notificationId) {
163+
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
164+
String appName = getAppName(this);
165+
166+
if (notificationId != 0) {
167+
Log.d(LOG_TAG, "cancel notification id: " + notificationId);
168+
setNotification(notificationId, "");
169+
try {
170+
notificationManager.cancel(appName, notificationId);
171+
} catch (NullPointerException e) {
172+
Log.e(LOG_TAG, "could not cancel notification id: " + notificationId);
173+
}
174+
}
175+
}
176+
142177

143178
/*
144179
* 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
@@ -541,10 +541,13 @@ private void clearAllNotifications() {
541541
}
542542

543543
private void clearNotification(int id) {
544-
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
545-
.getSystemService(Context.NOTIFICATION_SERVICE);
546-
String appName = (String) this.cordova.getActivity().getPackageManager()
547-
.getApplicationLabel(this.cordova.getActivity().getApplicationInfo());
544+
PushPlugin.clearNotification(cordova.getActivity(), id);
545+
}
546+
547+
static void clearNotification(Context context, int id) {
548+
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
549+
String appName = (String) context.getPackageManager()
550+
.getApplicationLabel(context.getApplicationInfo());
548551
notificationManager.cancel(appName, id);
549552
}
550553

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(), ^{
@@ -124,13 +126,15 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
124126
pushHandler.tapped = false;
125127
[pushHandler notificationReceived];
126128
} else {
129+
[self clearNotifications:userInfo];
127130
NSLog(@"just put it in the shade");
128131
//save it for later
129132
self.launchNotification = userInfo;
130133
completionHandler(UIBackgroundFetchResultNewData);
131134
}
132135

133136
} else {
137+
[self clearNotifications:userInfo];
134138
completionHandler(UIBackgroundFetchResultNoData);
135139
}
136140
}
@@ -188,6 +192,33 @@ - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification {
188192
[[NSNotificationCenter defaultCenter] postNotificationName:pushPluginApplicationDidBecomeActiveNotification object:nil];
189193
}
190194

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

203-
completionHandler(UNNotificationPresentationOptionNone);
234+
completionHandler(UNNotificationPresentationOptionBadge);
204235
}
205236

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

src/ios/PushPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- (void)subscribe:(CDVInvokedUrlCommand*)command;
6363
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
6464
- (void)clearNotification:(CDVInvokedUrlCommand*)command;
65+
- (void)clearRealNotification:(NSNumber*)notId;
6566

6667
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
6768
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

src/ios/PushPlugin.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ - (void)notificationReceived {
474474
}
475475
}
476476

477-
- (void)clearNotification:(CDVInvokedUrlCommand *)command
477+
- (void)clearRealNotification:(NSNumber *)notId
478478
{
479-
NSNumber *notId = [command.arguments objectAtIndex:0];
480479
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
481480
/*
482481
* 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
@@ -489,13 +488,19 @@ - (void)clearNotification:(CDVInvokedUrlCommand *)command
489488
[matchingNotificationIdentifiers addObject:notification.request.identifier];
490489
}
491490
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:matchingNotificationIdentifiers];
492-
493-
NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId];
494-
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
495-
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
496491
}];
497492
}
498493

494+
- (void)clearNotification:(CDVInvokedUrlCommand *)command
495+
{
496+
NSNumber *notId = [command.arguments objectAtIndex:0];
497+
[self clearRealNotification: notId];
498+
499+
NSString *message = [NSString stringWithFormat:@"Cleared notification with ID: %@", notId];
500+
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
501+
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
502+
}
503+
499504
- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command
500505
{
501506
NSMutableDictionary* options = [command.arguments objectAtIndex:0];

0 commit comments

Comments
 (0)