RNNotifications.m 2.54 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
    RNEventEmitter* _eventEmitter;
    RNNotificationsStore* _store;
}

- (instancetype)init {
    self = [super init];
    _store = [RNNotificationsStore new];
yogevbd's avatar
yogevbd committed
21
    _notificationEventHandler = [[RNNotificationEventHandler alloc] initWithStore:_store];
yogevbd's avatar
WIP  
yogevbd committed
22
    return self;
23
}
24

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

yogevbd's avatar
yogevbd committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
+ (void)startMonitorNotifications {
    [[self sharedInstance] startMonitorNotifications];
}

+ (void)startMonitorPushKitNotifications {
    [[self sharedInstance] startMonitorPushKitNotifications];
}

+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
    [[self sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [[self sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)startMonitorNotifications {
yogevbd's avatar
WIP  
yogevbd committed
52
    _notificationCenterListener = [[RNNotificationCenterListener alloc] initWithNotificationEventHandler:_notificationEventHandler];
Artal Druk's avatar
Artal Druk committed
53
}
54

yogevbd's avatar
yogevbd committed
55
- (void)startMonitorPushKitNotifications {
yogevbd's avatar
WIP  
yogevbd committed
56 57 58 59
    _pushKitEventHandler = [RNPushKitEventHandler new];
    _pushKit = [[RNPushKit alloc] initWithEventHandler:_pushKitEventHandler];
}

yogevbd's avatar
WIP  
yogevbd committed
60
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
yogevbd's avatar
WIP  
yogevbd committed
61
    [_notificationEventHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
62
}
Lidan Hifi's avatar
Lidan Hifi committed
63

yogevbd's avatar
WIP  
yogevbd committed
64
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
yogevbd's avatar
WIP  
yogevbd committed
65
    [_notificationEventHandler didFailToRegisterForRemoteNotificationsWithError:error];
66
}
Lidan Hifi's avatar
Lidan Hifi committed
67

yogevbd's avatar
WIP  
yogevbd committed
68 69 70 71
- (void)setInitialNotification:(NSDictionary *)notification {
    [_store setInitialNotification:notification];
}

yogevbd's avatar
WIP  
yogevbd committed
72 73 74 75 76 77
- (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
78 79
}

Lidan Hifi's avatar
Lidan Hifi committed
80
@end