Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 110e3c2

Browse files
merrygobyebyemacdonst
authored andcommitted
🍎 ✨ 📝 Issue #2191: Implement the ability to clear one notification on iOS and update documentation (#2478)
1 parent d5d2e9d commit 110e3c2

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

docs/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- [push.getApplicationIconBadgeNumber() - iOS & Android only](#pushgetapplicationiconbadgenumbersuccesshandler-errorhandler---ios--android-only)
1818
- [push.finish() - iOS only](#pushfinishsuccesshandler-errorhandler-id---ios-only)
1919
- [push.clearAllNotifications() - iOS & Android only](#pushclearallnotificationssuccesshandler-errorhandler---ios--android-only)
20-
- [push.clearNotification() - Android only](#pushclearnotificationid-successhandler-errorhandler---android-only)
20+
- [push.clearNotification() - iOS & Android only](#pushclearnotificationid-successhandler-errorhandler---ios--android-only)
2121

2222
## PushNotification.init(options)
2323

@@ -592,7 +592,7 @@ push.clearAllNotifications(
592592
);
593593
```
594594

595-
## push.clearNotification(id, successHandler, errorHandler) - Android only
595+
## push.clearNotification(id, successHandler, errorHandler) - iOS & Android only
596596

597597
Tells the OS to clear the notification that corresponds to the id argument, from the Notification Center
598598

docs/PAYLOAD.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ The JSON message can contain the following fields, see [Apple developer docs](ht
161161
"thread-id": "id", // Provide this key with a string value that represents the app-specific identifier for grouping notifications
162162
"sound": "default" // play default sound, or custom sound, see [iOS Sound](#sound-1) section
163163
},
164+
"notId": 1,
164165
"custom_key1": "value1",
165166
"custom_key2": "value2"
166167
}
@@ -173,7 +174,7 @@ This is the JSON-encoded format you can send via AWS-SNS's web UI:
173174
```json
174175
{
175176
"APNS_SANDBOX":
176-
"{\"aps\":{\"alert\":{\"title\":\"A short string describing the purpose of the notification\",\"body\":\"The text of the alert message\",\"launch-image\":\"The filename of an image file in the app bundle, with or without the filename extension. The image is used as the launch image when users tap the action button or move the action slider\"},\"badge\":5,\"content-available\":\"0\",\"category\":\"identifier\",\"thread-id\":\"id\",\"sound\":\"default\"},\"custom_key1\":\"value1\",\"custom_key2\":\"value2\"}"
177+
"{\"aps\":{\"alert\":{\"title\":\"A short string describing the purpose of the notification\",\"body\":\"The text of the alert message\",\"launch-image\":\"The filename of an image file in the app bundle, with or without the filename extension. The image is used as the launch image when users tap the action button or move the action slider\"},\"badge\":5,\"content-available\":\"0\",\"category\":\"identifier\",\"thread-id\":\"id\",\"sound\":\"default\"},\"notId\":1,\"custom_key1\":\"value1\",\"custom_key2\":\"value2\"}"
177178
}
178179
```
179180

@@ -203,6 +204,7 @@ Note that the properties are "normalized" accross platforms, so this is passed t
203204
"coldstart": false,
204205
"foreground": false,
205206
"content-available": "0",
207+
"notId": 1,
206208
"custom_key1": "value1",
207209
"custom_key2": "value2",
208210
"launch-image": "The filename of an image file in the app bundle, with or without the filename extension. The image is used as the launch image when users tap the action button or move the action slider",

src/ios/PushPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- (void)unregister:(CDVInvokedUrlCommand*)command;
6060
- (void)subscribe:(CDVInvokedUrlCommand*)command;
6161
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
62+
- (void)clearNotification:(CDVInvokedUrlCommand*)command;
6263

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

src/ios/PushPlugin.m

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

469+
- (void)clearNotification:(CDVInvokedUrlCommand *)command
470+
{
471+
NSNumber *notId = [command.arguments objectAtIndex:0];
472+
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
473+
/*
474+
* 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
475+
* all notifications with the same value for "notId"
476+
*/
477+
NSPredicate *matchingNotificationPredicate = [NSPredicate predicateWithFormat:@"request.content.userInfo.notId == %@", notId];
478+
NSArray<UNNotification *> *matchingNotifications = [notifications filteredArrayUsingPredicate:matchingNotificationPredicate];
479+
NSMutableArray<NSString *> *matchingNotificationIdentifiers = [NSMutableArray array];
480+
for (UNNotification *notification in matchingNotifications) {
481+
[matchingNotificationIdentifiers addObject:notification.request.identifier];
482+
}
483+
[[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];
488+
}];
489+
}
490+
469491
- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command
470492
{
471493
NSMutableDictionary* options = [command.arguments objectAtIndex:0];

0 commit comments

Comments
 (0)