diff --git a/native/ios/Comm/CommIOSNotifications/CommIOSNotifications.mm b/native/ios/Comm/CommIOSNotifications/CommIOSNotifications.mm --- a/native/ios/Comm/CommIOSNotifications/CommIOSNotifications.mm +++ b/native/ios/Comm/CommIOSNotifications/CommIOSNotifications.mm @@ -22,6 +22,29 @@ NSString *const CommIOSNotificationsActionTriggered = @"CommIOSNotificationsActionTriggered"; +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[@"fireDate"] = dateString; + } + + formattedNotification[@"alertTitle"] = RCTNullIfNil(content.title); + formattedNotification[@"alertBody"] = RCTNullIfNil(content.body); + formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier); + formattedNotification[@"thread-id"] = RCTNullIfNil(content.threadIdentifier); + formattedNotification[@"userInfo"] = + RCTNullIfNil(RCTJSONClean(content.userInfo)); + + return formattedNotification; +} + @interface CommIOSNotifications () @property(nonatomic, strong) NSMutableDictionary *remoteNotificationCallbacks; @end @@ -384,6 +407,25 @@ [CommIOSNotifications requestPermissions]; } +RCT_EXPORT_METHOD(completeNotif + : (NSString *)completionKey fetchResult + : (UIBackgroundFetchResult)result) { + RCTRemoteNotificationCallback completionHandler = + self.remoteNotificationCallbacks[completionKey]; + if (!completionHandler) { + NSLog(@"There is no completion handler with key: %@", completionKey); + return; + } + completionHandler(result); + [self.remoteNotificationCallbacks removeObjectForKey:completionKey]; +} + +RCT_EXPORT_METHOD(setBadgesCount : (int)count) { + dispatch_async(dispatch_get_main_queue(), ^{ + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count]; + }); +} + RCT_EXPORT_METHOD(consumeBackgroundQueue) { // Mark JS Thread as ready [CommIOSNotificationsBridgeQueue sharedInstance].jsIsReady = YES; @@ -440,4 +482,40 @@ }); } +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 ]); + }]; + } +} + @end