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 @@ -21,6 +21,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; @property(nonatomic) BOOL hasListeners; @@ -304,6 +327,25 @@ }]; } +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].jsReady = YES; @@ -328,8 +370,8 @@ }]; // Push opened remote notifications - NSDictionary *openedRemoteNotification = - [CommIOSNotificationsBridgeQueue sharedInstance].openedRemoteNotification; + NSDictionary *openedRemoteNotification = [_bridge.launchOptions + objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (openedRemoteNotification) { [CommIOSNotificationsBridgeQueue sharedInstance].openedRemoteNotification = nil; @@ -357,4 +399,40 @@ resolve(permissions); } +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