RNCommandsHandler.m 4.75 KB
Newer Older
yogevbd's avatar
WIP  
yogevbd committed
1 2 3
#import "RNCommandsHandler.h"
#import "RNNotifications.h"
#import "RCTConvert+Notifications.h"
4
#import "RNPushKit.h"
yogevbd's avatar
WIP  
yogevbd committed
5

6 7
@implementation RNCommandsHandler {
    RNPushKit* _pushKit;
yogevbd's avatar
WIP  
yogevbd committed
8 9 10
}

- (void)requestPermissionsWithCategories:(NSArray *)json {
11
    NSMutableSet<UNNotificationCategory *>* categories = nil;
yogevbd's avatar
WIP  
yogevbd committed
12 13 14 15
    
    if ([json count] > 0) {
        categories = [NSMutableSet new];
        for (NSDictionary* categoryJson in json) {
16
            [categories addObject:[RCTConvert UNMutableUserNotificationCategory:categoryJson]];
yogevbd's avatar
WIP  
yogevbd committed
17 18
        }
    }
19 20 21 22 23 24 25 26 27
    [[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) {
yogevbd's avatar
WIP  
yogevbd committed
28 29 30
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [[UIApplication sharedApplication] registerForRemoteNotifications];
                        });
31 32 33 34 35 36 37
                    }
                }];
            } else {
                
            }
        }
    }];
yogevbd's avatar
WIP  
yogevbd committed
38 39 40
}

- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
yogevbd's avatar
WIP  
yogevbd committed
41
    resolve([[RNNotifications sharedInstance] initialNotification]);
yogevbd's avatar
WIP  
yogevbd committed
42 43 44
}

- (void)completionHandler:(NSString *)completionKey {
yogevbd's avatar
WIP  
yogevbd committed
45
    [[RNNotifications sharedInstance] finishHandleNotificationKey:completionKey];
yogevbd's avatar
WIP  
yogevbd committed
46 47 48 49 50 51 52
}

- (void)abandonPermissions {
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}

- (void)registerPushKit {
53
    _pushKit = [[RNPushKit alloc] initWithPushKitEventListener:[RNPushKitEventListener new]];
yogevbd's avatar
WIP  
yogevbd committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

- (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<NSString *> *)identifiers {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center removeDeliveredNotificationsWithIdentifiers:identifiers];
}

- (void)getDeliveredNotifications:(RCTResponseSenderBlock)callback {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
        NSMutableArray<NSDictionary *> *formattedNotifications = [NSMutableArray new];
        
        for (UNNotification *notification in notifications) {
            [formattedNotifications addObject:RCTFormatUNNotification(notification)];
        }
        callback(@[formattedNotifications]);
    }];
}

@end