RNNotificationEventHandler.m 1.7 KB
Newer Older
1 2
#import "RNNotificationEventHandler.h"
#import "RNEventEmitter.h"
3 4
#import "RNNotificationUtils.h"
#import "RCTConvert+RNNotifications.h"
yogevbd's avatar
WIP  
yogevbd committed
5
#import "RNNotificationParser.h"
6

yogevbd's avatar
WIP  
yogevbd committed
7 8 9 10 11 12 13 14 15
@implementation RNNotificationEventHandler {
    RNNotificationsStore* _store;
}

- (instancetype)initWithStore:(RNNotificationsStore *)store {
    self = [super init];
    _store = store;
    return self;
}
16

yogevbd's avatar
WIP  
yogevbd committed
17
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
18
    NSString *tokenRepresentation = [deviceToken isKindOfClass:[NSString class]] ? deviceToken : [RNNotificationUtils deviceTokenToString:deviceToken];
yogevbd's avatar
WIP  
yogevbd committed
19 20 21 22 23 24 25
    [RNEventEmitter sendEvent:RNRegistered body:@{@"deviceToken": tokenRepresentation}];
}

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [RNEventEmitter sendEvent:RNRegistrationFailed body:@{@"code": [NSNumber numberWithInteger:error.code], @"domain": error.domain, @"localizedDescription": error.localizedDescription}];
}

yogevbd's avatar
WIP  
yogevbd committed
26 27 28
- (void)didReceiveForegroundNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    [_store setPresentationCompletionHandler:completionHandler withCompletionKey:notification.request.identifier];
    [RNEventEmitter sendEvent:RNNotificationReceivedForeground body:[RNNotificationParser parseNotification:notification]];
29 30
}

yogevbd's avatar
yogevbd committed
31
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(void))completionHandler {
yogevbd's avatar
WIP  
yogevbd committed
32 33
    [_store setActionCompletionHandler:completionHandler withCompletionKey:response.notification.request.identifier];
    [RNEventEmitter sendEvent:RNNotificationOpened body:[RNNotificationParser parseNotificationResponse:response]];
34 35 36
}

@end