RNNotificationEventHandler.m 1.66 KB
Newer Older
1 2 3
#import "RNNotificationEventHandler.h"
#import "RNEventEmitter.h"

yogevbd's avatar
WIP  
yogevbd committed
4 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 15

- (void)didReceiveForegroundPayload:(NSDictionary *)payload {
yogevbd's avatar
WIP  
yogevbd committed
16
    [RNEventEmitter sendEvent:RNNotificationReceivedForeground body:payload];
17 18 19
}

- (void)didOpenNotificationPayload:(NSDictionary *)payload {
yogevbd's avatar
WIP  
yogevbd committed
20
    [RNEventEmitter sendEvent:RNNotificationOpened body:payload];
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
}

- (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
40 41
    [_store setCompletionHandler:completionHandler withCompletionKey:identifier];
    [RNEventEmitter sendEvent:RNActionTriggered body:userInfo];
42 43 44
}

@end