RNNotifications.m 2.27 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1 2

#import <UIKit/UIKit.h>
3
#import <PushKit/PushKit.h>
4 5
#import <React/RCTBridge.h>
#import "RNNotifications.h"
yogevbd's avatar
WIP  
yogevbd committed
6
#import "RNNotificationCenterListener.h"
yogevbd's avatar
WIP  
yogevbd committed
7
#import "RNPushKit.h"
Lidan Hifi's avatar
Lidan Hifi committed
8

9
@implementation RNNotifications {
yogevbd's avatar
WIP  
yogevbd committed
10
    RNPushKit* _pushKit;
yogevbd's avatar
WIP  
yogevbd committed
11
    RNNotificationCenterListener* _notificationCenterListener;
yogevbd's avatar
WIP  
yogevbd committed
12
    RNNotificationEventHandler* _notificationEventHandler;
yogevbd's avatar
WIP  
yogevbd committed
13
    RNPushKitEventHandler* _pushKitEventHandler;
yogevbd's avatar
WIP  
yogevbd committed
14 15 16 17 18 19 20 21
    RNEventEmitter* _eventEmitter;
    RNNotificationsStore* _store;
}

- (instancetype)init {
    self = [super init];
    _store = [RNNotificationsStore new];
    return self;
22
}
23

yogevbd's avatar
WIP  
yogevbd committed
24 25 26 27 28 29 30 31 32 33
+ (instancetype)sharedInstance {
    static RNNotifications *sharedInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[RNNotifications alloc] init];
    });
    return sharedInstance;
}

34
- (void)initialize {
yogevbd's avatar
WIP  
yogevbd committed
35
    _notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store];
yogevbd's avatar
WIP  
yogevbd committed
36
    _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler];
Artal Druk's avatar
Artal Druk committed
37
}
38

yogevbd's avatar
WIP  
yogevbd committed
39 40 41 42 43
- (void)initializePushKit {
    _pushKitEventHandler = [RNPushKitEventHandler new];
    _pushKit = [[RNPushKit alloc] initWithEventHandler:_pushKitEventHandler];
}

yogevbd's avatar
WIP  
yogevbd committed
44
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
yogevbd's avatar
WIP  
yogevbd committed
45
    [_notificationEventHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
46
}
Lidan Hifi's avatar
Lidan Hifi committed
47

yogevbd's avatar
WIP  
yogevbd committed
48
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
yogevbd's avatar
WIP  
yogevbd committed
49
    [_notificationEventHandler didFailToRegisterForRemoteNotificationsWithError:error];
50
}
Lidan Hifi's avatar
Lidan Hifi committed
51

yogevbd's avatar
WIP  
yogevbd committed
52 53 54 55 56 57
- (void)setBadgeForNotification:(NSDictionary *)notification {
    if ([[notification objectForKey:@"aps"] objectForKey:@"badge"]){
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[notification objectForKey:@"aps"] objectForKey:@"badge"] intValue]];
    }
}

yogevbd's avatar
WIP  
yogevbd committed
58 59 60 61
- (void)setInitialNotification:(NSDictionary *)notification {
    [_store setInitialNotification:notification];
}

yogevbd's avatar
WIP  
yogevbd committed
62 63 64 65 66 67
- (void)finishHandleActionKey:(NSString *)actionKey {
    [_store completeAction:actionKey];
}

- (void)finishHandleNotificationKey:(NSString *)notificationKey presentingOptions:(UNNotificationPresentationOptions)presentingOptions {
    [_store completePresentation:notificationKey withPresentationOptions:presentingOptions];
yogevbd's avatar
WIP  
yogevbd committed
68 69
}

Lidan Hifi's avatar
Lidan Hifi committed
70
@end