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 @@ -340,4 +340,104 @@ return [result copy]; } ++ (void)requestPermissions { + UNUserNotificationCenter *center = + [UNUserNotificationCenter currentNotificationCenter]; + UNAuthorizationOptions options = UNAuthorizationOptionAlert + + UNAuthorizationOptionSound + UNAuthorizationOptionBadge; + [center + requestAuthorizationWithOptions:options + completionHandler:^( + BOOL granted, NSError *_Nullable error) { + if (!granted) { + NSDictionary *errorInfo; + if (error) { + errorInfo = @{ + @"code" : [NSNumber numberWithInteger:error.code], + @"domain" : error.domain, + @"localizedDescription" : error.localizedDescription + }; + } else { + errorInfo = @{}; + } + [[NSNotificationCenter defaultCenter] + postNotificationName: + CommIOSNotificationsRegistrationFailed + object:self + userInfo:errorInfo]; + return; + } + if ([UIApplication instancesRespondToSelector:@selector + (registerForRemoteNotifications)]) { + dispatch_async(dispatch_get_main_queue(), ^{ + [[UIApplication sharedApplication] + registerForRemoteNotifications]; + }); + } + }]; +} + +/* + React Native exported methods +*/ +RCT_EXPORT_METHOD(requestPermissions) { + [CommIOSNotifications requestPermissions]; +} + +RCT_EXPORT_METHOD(consumeBackgroundQueue) { + // Mark JS Thread as ready + [CommIOSNotificationsBridgeQueue sharedInstance].jsIsReady = YES; + + // Push actions to JS + [[CommIOSNotificationsBridgeQueue sharedInstance] + consumeActionsQueue:^(NSDictionary *action) { + [[NSNotificationCenter defaultCenter] + postNotificationName:CommIOSNotificationsActionTriggered + object:self + userInfo:action]; + }]; + + // Push background notifications to JS + [[CommIOSNotificationsBridgeQueue sharedInstance] + consumeNotificationsQueue:^(NSDictionary *notifInfo) { + NSDictionary *notification = notifInfo[@"notification"]; + RCTRemoteNotificationCallback completionHandler = + notifInfo[@"completionHandler"]; + [CommIOSNotifications didReceiveRemoteNotification:notification + fetchCompletionHandler:completionHandler]; + }]; + + // Push opened local notifications + NSDictionary *openedLocalNotification = + [CommIOSNotificationsBridgeQueue sharedInstance].openedLocalNotification; + if (openedLocalNotification) { + [CommIOSNotificationsBridgeQueue sharedInstance].openedLocalNotification = + nil; + NSDictionary *notifInfo = @{@"notification" : openedLocalNotification}; + [CommIOSNotifications didNotificationOpen:notifInfo]; + } + + // Push opened remote notifications + NSDictionary *openedRemoteNotification = + [CommIOSNotificationsBridgeQueue sharedInstance].openedRemoteNotification; + if (openedRemoteNotification) { + [CommIOSNotificationsBridgeQueue sharedInstance].openedRemoteNotification = + nil; + NSDictionary *notifInfo = @{@"notification" : openedRemoteNotification}; + [CommIOSNotifications didNotificationOpen:notifInfo]; + } +} + +RCT_EXPORT_METHOD(checkPermissions + : (RCTPromiseResolveBlock)resolve reject + : (RCTPromiseRejectBlock)reject) { + UIUserNotificationSettings *currentSettings = + [[UIApplication sharedApplication] currentUserNotificationSettings]; + resolve(@{ + @"badge" : @((currentSettings.types & UIUserNotificationTypeBadge) > 0), + @"sound" : @((currentSettings.types & UIUserNotificationTypeSound) > 0), + @"alert" : @((currentSettings.types & UIUserNotificationTypeAlert) > 0), + }); +} + @end