RNNotificationEventHandler.m 2.24 KB
Newer Older
1 2
#import "RNNotificationEventHandler.h"
#import "RNEventEmitter.h"
yogevbd's avatar
WIP  
yogevbd committed
3
#import "RNUtils.h"
4

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

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

yogevbd's avatar
WIP  
yogevbd committed
15 16 17 18 19 20 21 22 23
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(id)deviceToken {
    NSString *tokenRepresentation = [deviceToken isKindOfClass:[NSString class]] ? deviceToken : [RNUtils deviceTokenToString:deviceToken];
    [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}];
}

24
- (void)didReceiveForegroundPayload:(NSDictionary *)payload {
yogevbd's avatar
WIP  
yogevbd committed
25
    [RNEventEmitter sendEvent:RNNotificationReceivedForeground body:payload];
26 27 28
}

- (void)didOpenNotificationPayload:(NSDictionary *)payload {
yogevbd's avatar
WIP  
yogevbd committed
29
    [RNEventEmitter sendEvent:RNNotificationOpened body:payload];
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

- (void)handleActionWithIdentifier:(NSString *)identifier forPayload:(NSDictionary *)payload withResponse:(NSString *)response completionHandler:(void (^)())completionHandler {
    [self emitNotificationActionForIdentifier:identifier response:response userInfo:payload completionHandler:completionHandler];
}

- (void)emitNotificationActionForIdentifier:(NSString *)identifier response:(NSString *)response userInfo:(NSDictionary *)userInfo  completionHandler:(void (^)())completionHandler {
    NSString* completionKey = [NSString stringWithFormat:@"%@.%@", identifier, [NSString stringWithFormat:@"%ldd", (long)[[NSDate date] timeIntervalSince1970]]];
    NSMutableDictionary* info = [[NSMutableDictionary alloc] initWithDictionary:@{ @"identifier": identifier, @"completionKey": completionKey }];
    
    if (response != NULL) {
        info[@"text"] = response;
    }
    
    // add notification custom data
    if (userInfo != NULL) {
        info[@"notification"] = userInfo;
    }
    
yogevbd's avatar
WIP  
yogevbd committed
49 50
    [_store setCompletionHandler:completionHandler withCompletionKey:identifier];
    [RNEventEmitter sendEvent:RNActionTriggered body:userInfo];
51 52 53
}

@end