Skip to content

Commit 3df654e

Browse files
gperdomorfacebook-github-bot
authored andcommitted
Integrare UNUserNotification
Summary: Implementing removeAllDeliveredNotifications and removeDeliveredNotifications for remove notifications from notification center, and getDeliveredNotifications Thanks for submitting a PR! Please read these instructions carefully: - [x] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. Currently, calling PushNotificationIOS.cancelAllLocalNotifications not remove the notification from the Notification Center In iOS 10, a new UNUserNotification class was introduced, this class has a method which get and remove the notifications from notification center This PR try to solve that. In my case, i'm working with an messaging app, every message is a new notification, when the user tap a notification, the app is opened and the rest of notifications should be gon Closes #13036 Differential Revision: D4761828 Pulled By: javache fbshipit-source-id: 216e44a64f1bf88b5ae3045d1fa6eca8a1278a71
1 parent 9d377e9 commit 3df654e

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Libraries/PushNotificationIOS/PushNotificationIOS.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,40 @@ class PushNotificationIOS {
164164
RCTPushNotificationManager.cancelAllLocalNotifications();
165165
}
166166

167+
/**
168+
* Remove all delivered notifications from Notification Center
169+
*/
170+
static removeAllDeliveredNotifications(): void {
171+
RCTPushNotificationManager.removeAllDeliveredNotifications();
172+
}
173+
174+
/**
175+
* Provides you with a list of the app’s notifications that are still displayed in Notification Center
176+
*
177+
* @param callback Function which receive an array of delivered notifications
178+
*
179+
* A delivered notification is an object containing:
180+
*
181+
* - `identifier` : The identifier of this notification.
182+
* - `title` : The title of this notification.
183+
* - `body` : The body of this notification.
184+
* - `category` : The category of this notification, if has one.
185+
* - `userInfo` : An optional object containing additional notification data.
186+
* - `thread-id` : The thread identifier of this notification, if has one.
187+
*/
188+
static getDeliveredNotifications(callback: (notifications: [Object]) => void): void {
189+
RCTPushNotificationManager.getDeliveredNotifications(callback);
190+
}
191+
192+
/**
193+
* Removes the specified notifications from Notification Center
194+
*
195+
* @param identifiers Array of notification identifiers
196+
*/
197+
static removeDeliveredNotifications(identifiers: [string]): void {
198+
RCTPushNotificationManager.removeDeliveredNotifications(identifiers);
199+
}
200+
167201
/**
168202
* Sets the badge number for the app icon on the home screen
169203
*/

Libraries/PushNotificationIOS/RCTPushNotificationManager.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import "RCTPushNotificationManager.h"
1111

12+
#import <UserNotifications/UserNotifications.h>
13+
1214
#import <React/RCTBridge.h>
1315
#import <React/RCTConvert.h>
1416
#import <React/RCTEventDispatcher.h>
@@ -97,6 +99,29 @@ @implementation RCTPushNotificationManager
9799
return formattedLocalNotification;
98100
}
99101

102+
static NSDictionary *RCTFormatUNNotification(UNNotification *notification)
103+
{
104+
NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
105+
UNNotificationContent *content = notification.request.content;
106+
107+
formattedNotification[@"identifier"] = notification.request.identifier;
108+
109+
if (notification.date) {
110+
NSDateFormatter *formatter = [NSDateFormatter new];
111+
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
112+
NSString *dateString = [formatter stringFromDate:notification.date];
113+
formattedNotification[@"date"] = dateString;
114+
}
115+
116+
formattedNotification[@"title"] = RCTNullIfNil(content.title);
117+
formattedNotification[@"body"] = RCTNullIfNil(content.body);
118+
formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier);
119+
formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier);
120+
formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
121+
122+
return formattedNotification;
123+
}
124+
100125
#endif //TARGET_OS_TV
101126

102127
RCT_EXPORT_MODULE()
@@ -407,6 +432,37 @@ - (void)handleRegisterUserNotificationSettings:(NSNotification *)notification
407432
callback(@[formattedScheduledLocalNotifications]);
408433
}
409434

435+
RCT_EXPORT_METHOD(removeAllDeliveredNotifications)
436+
{
437+
if ([UNUserNotificationCenter class]) {
438+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
439+
[center removeAllDeliveredNotifications];
440+
}
441+
}
442+
443+
RCT_EXPORT_METHOD(removeDeliveredNotifications:(NSArray<NSString *> *)identifiers)
444+
{
445+
if ([UNUserNotificationCenter class]) {
446+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
447+
[center removeDeliveredNotificationsWithIdentifiers:identifiers];
448+
}
449+
}
450+
451+
RCT_EXPORT_METHOD(getDeliveredNotifications:(RCTResponseSenderBlock)callback)
452+
{
453+
if ([UNUserNotificationCenter class]) {
454+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
455+
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *_Nonnull notifications) {
456+
NSMutableArray<NSDictionary *> *formattedNotifications = [NSMutableArray new];
457+
458+
for (UNNotification *notification in notifications) {
459+
[formattedNotifications addObject:RCTFormatUNNotification(notification)];
460+
}
461+
callback(@[formattedNotifications]);
462+
}];
463+
}
464+
}
465+
410466
#endif //TARGET_OS_TV
411467

412468
@end

0 commit comments

Comments
 (0)