RNCommandsHandler.m 4.84 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
    RNNotificationsStore* _store;
9
}
yogevbd's avatar
WIP  
yogevbd committed
10

yogevbd's avatar
WIP  
yogevbd committed
11
- (instancetype)initWithStore:(RNNotificationsStore *)store {
yogevbd's avatar
WIP  
yogevbd committed
12
    self = [super init];
yogevbd's avatar
WIP  
yogevbd committed
13 14
    _store = store;
    
yogevbd's avatar
WIP  
yogevbd committed
15 16 17 18
    return self;
}

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

- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
yogevbd's avatar
WIP  
yogevbd committed
49
    resolve(_store.initialNotification);
yogevbd's avatar
WIP  
yogevbd committed
50 51 52
}

- (void)completionHandler:(NSString *)completionKey {
yogevbd's avatar
WIP  
yogevbd committed
53
    [_store completeAction:completionKey];
yogevbd's avatar
WIP  
yogevbd committed
54 55 56 57 58 59 60
}

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

- (void)registerPushKit {
61
    _pushKit = [[RNPushKit alloc] initWithPushKitEventListener:[RNPushKitEventListener new]];
yogevbd's avatar
WIP  
yogevbd committed
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 116 117 118 119 120 121 122 123
}

- (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