#import "RNCommandsHandler.h" #import "RNNotifications.h" #import "RCTConvert+Notifications.h" #import "RNPushKit.h" @implementation RNCommandsHandler { RNPushKit* _pushKit; RNNotificationsStore* _store; } - (instancetype)initWithStore:(RNNotificationsStore *)store { self = [super init]; _store = store; return self; } - (void)requestPermissionsWithCategories:(NSArray *)json { NSMutableSet* categories = nil; if ([json count] > 0) { categories = [NSMutableSet new]; for (NSDictionary* categoryJson in json) { [categories addObject:[RCTConvert UNMutableUserNotificationCategory:categoryJson]]; } } [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories]; UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert); [UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { if (error) { } else { if (granted) { [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } }]; } else { } } }]; } - (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { resolve(_store.initialNotification); } - (void)completionHandler:(NSString *)completionKey { [_store completeAction:completionKey]; } - (void)abandonPermissions { [[UIApplication sharedApplication] unregisterForRemoteNotifications]; } - (void)registerPushKit { _pushKit = [[RNPushKit alloc] initWithPushKitEventListener:[RNPushKitEventListener new]]; } - (void)getBadgesCount:(RCTResponseSenderBlock)callback { NSInteger count = [UIApplication sharedApplication].applicationIconBadgeNumber; callback(@[ [NSNumber numberWithInteger:count] ]); } - (void)setBadgesCount:(int)count { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count]; } - (void)localNotification:(NSDictionary *)notification withId:(NSString *)notificationId { UNNotificationRequest* localNotification = [RCTConvert UNNotificationRequest:notification withId:notificationId]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:localNotification withCompletionHandler:nil]; } - (void)cancelLocalNotification:(NSString *)notificationId { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removePendingNotificationRequestsWithIdentifiers:@[notificationId]]; } - (void)cancelAllLocalNotifications { [RCTSharedApplication() cancelAllLocalNotifications]; } - (void)isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { BOOL ans = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != 0; resolve(@(ans)); } - (void)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), }); } - (void)removeAllDeliveredNotifications { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllDeliveredNotifications]; } - (void)removeDeliveredNotifications:(NSArray *)identifiers { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeDeliveredNotificationsWithIdentifiers:identifiers]; } - (void)getDeliveredNotifications:(RCTResponseSenderBlock)callback { 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