diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 4ff179559..dfee28a32 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -164,6 +164,40 @@ class PushNotificationIOS { RCTPushNotificationManager.cancelAllLocalNotifications(); } + /** + * Remove all delivered notifications from Notification Center + */ + static removeAllDeliveredNotifications(): void { + RCTPushNotificationManager.removeAllDeliveredNotifications(); + } + + /** + * Provides you with a list of the app’s notifications that are still displayed in Notification Center + * + * @param callback Function which receive an array of delivered notifications + * + * A delivered notification is an object containing: + * + * - `identifier` : The identifier of this notification. + * - `title` : The title of this notification. + * - `body` : The body of this notification. + * - `category` : The category of this notification, if has one. + * - `userInfo` : An optional object containing additional notification data. + * - `thread-id` : The thread identifier of this notification, if has one. + */ + static getDeliveredNotifications(callback: (notifications: [Object]) => void): void { + RCTPushNotificationManager.getDeliveredNotifications(callback); + } + + /** + * Removes the specified notifications from Notification Center + * + * @param identifiers Array of notification identifiers + */ + static removeDeliveredNotifications(identifiers: [string]): void { + RCTPushNotificationManager.removeDeliveredNotifications(identifiers); + } + /** * Sets the badge number for the app icon on the home screen */ diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 216c87dd5..b57fd2b72 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -9,6 +9,8 @@ #import "RCTPushNotificationManager.h" +#import + #import #import #import @@ -97,6 +99,29 @@ static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notificatio return formattedLocalNotification; } +static NSDictionary *RCTFormatUNNotification(UNNotification *notification) +{ + NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary]; + UNNotificationContent *content = notification.request.content; + + formattedNotification[@"identifier"] = notification.request.identifier; + + if (notification.date) { + NSDateFormatter *formatter = [NSDateFormatter new]; + [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]; + NSString *dateString = [formatter stringFromDate:notification.date]; + formattedNotification[@"date"] = dateString; + } + + formattedNotification[@"title"] = RCTNullIfNil(content.title); + formattedNotification[@"body"] = RCTNullIfNil(content.body); + formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier); + formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier); + formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo)); + + return formattedNotification; +} + #endif //TARGET_OS_TV RCT_EXPORT_MODULE() @@ -407,6 +432,37 @@ RCT_EXPORT_METHOD(getScheduledLocalNotifications:(RCTResponseSenderBlock)callbac callback(@[formattedScheduledLocalNotifications]); } +RCT_EXPORT_METHOD(removeAllDeliveredNotifications) +{ + if ([UNUserNotificationCenter class]) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center removeAllDeliveredNotifications]; + } +} + +RCT_EXPORT_METHOD(removeDeliveredNotifications:(NSArray *)identifiers) +{ + if ([UNUserNotificationCenter class]) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center removeDeliveredNotificationsWithIdentifiers:identifiers]; + } +} + +RCT_EXPORT_METHOD(getDeliveredNotifications:(RCTResponseSenderBlock)callback) +{ + if ([UNUserNotificationCenter class]) { + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center getDeliveredNotificationsWithCompletionHandler:^(NSArray *_Nonnull notifications) { + NSMutableArray *formattedNotifications = [NSMutableArray new]; + + for (UNNotification *notification in notifications) { + [formattedNotifications addObject:RCTFormatUNNotification(notification)]; + } + callback(@[formattedNotifications]); + }]; + } +} + #endif //TARGET_OS_TV @end