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

5
@implementation RNCommandsHandler {
yogevbd's avatar
WIP  
yogevbd committed
6 7 8 9 10 11 12
    RNNotificationCenter* _notificationCenter;
}

- (instancetype)init {
    self = [super init];
    _notificationCenter = [RNNotificationCenter new];
    return self;
yogevbd's avatar
WIP  
yogevbd committed
13 14 15
}

- (void)requestPermissionsWithCategories:(NSArray *)json {
yogevbd's avatar
WIP  
yogevbd committed
16
    [_notificationCenter requestPermissionsWithCategories:json];
yogevbd's avatar
WIP  
yogevbd committed
17 18 19
}

- (void)getInitialNotification:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
yogevbd's avatar
WIP  
yogevbd committed
20
    resolve([[RNNotifications sharedInstance] initialNotification]);
yogevbd's avatar
WIP  
yogevbd committed
21 22
}

yogevbd's avatar
WIP  
yogevbd committed
23 24 25 26 27 28
- (void)finishHandlingAction:(NSString *)completionKey {
    [[RNNotifications sharedInstance] finishHandleActionKey:completionKey];
}

- (void)finishPresentingNotification:(NSString *)completionKey presentingOptions:(NSDictionary *)presentingOptions {
    [[RNNotifications sharedInstance] finishHandleNotificationKey:completionKey presentingOptions:[RCTConvert UNNotificationPresentationOptions:presentingOptions]];
yogevbd's avatar
WIP  
yogevbd committed
29 30 31 32 33 34 35
}

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

- (void)registerPushKit {
yogevbd's avatar
WIP  
yogevbd committed
36
    [[RNNotifications sharedInstance] initializePushKit];
yogevbd's avatar
WIP  
yogevbd committed
37 38 39 40 41 42 43 44 45 46 47
}

- (void)getBadgesCount:(RCTResponseSenderBlock)callback {
    NSInteger count = [UIApplication sharedApplication].applicationIconBadgeNumber;
    callback(@[ [NSNumber numberWithInteger:count] ]);
}

- (void)setBadgesCount:(int)count {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
}

yogevbd's avatar
WIP  
yogevbd committed
48 49
- (void)sendLocalNotification:(NSDictionary *)notification withId:(NSString *)notificationId {
    [_notificationCenter sendLocalNotification:notification withId:notificationId];
yogevbd's avatar
WIP  
yogevbd committed
50 51 52
}

- (void)cancelLocalNotification:(NSString *)notificationId {
yogevbd's avatar
WIP  
yogevbd committed
53
    [_notificationCenter cancelLocalNotification:notificationId];
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
}

- (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 {
yogevbd's avatar
WIP  
yogevbd committed
75
    [_notificationCenter removeAllDeliveredNotifications];
yogevbd's avatar
WIP  
yogevbd committed
76 77 78
}

- (void)removeDeliveredNotifications:(NSArray<NSString *> *)identifiers {
yogevbd's avatar
WIP  
yogevbd committed
79
    [_notificationCenter removeDeliveredNotifications:identifiers];
yogevbd's avatar
WIP  
yogevbd committed
80 81 82
}

- (void)getDeliveredNotifications:(RCTResponseSenderBlock)callback {
yogevbd's avatar
WIP  
yogevbd committed
83
    [_notificationCenter getDeliveredNotifications:callback];
yogevbd's avatar
WIP  
yogevbd committed
84 85 86
}

@end