From c05a175033100c102ae376ef00a1739f7b04f5d9 Mon Sep 17 00:00:00 2001 From: Krystof Celba Date: Mon, 5 Feb 2018 09:31:05 +0100 Subject: [PATCH] Rewrite handling of actions when JS is not yet initialized. We have to save initial notification action response in case there isn't registered any FCMNotificationReceived event handler yet. For example when the app is killed by a user so JS has to be initialized first. The saved action response will be sent to JS after first FCMNotificationReceived event handler registered. --- ios/RNFIRMessaging.m | 47 +++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/ios/RNFIRMessaging.m b/ios/RNFIRMessaging.m index e572405..507b975 100644 --- a/ios/RNFIRMessaging.m +++ b/ios/RNFIRMessaging.m @@ -217,26 +217,7 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationCategoryOptions, (@{ @end -@interface RNFIRMessagingHelper : NSObject - -@property (nonatomic, retain) NSDictionary *lastNotificationResponse; - -+ (nonnull instancetype) sharedInstance; - -@end - -@implementation RNFIRMessagingHelper - -+ (nonnull instancetype)sharedInstance { - static RNFIRMessagingHelper *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [self new]; - }); - return sharedInstance; -} - -@end +static NSDictionary *initialNotificationActionResponse; @interface RNFIRMessaging () @property (nonatomic, strong) NSMutableDictionary *notificationCallbacks; @@ -282,8 +263,14 @@ RCT_EXPORT_MODULE(); } NSDictionary *userInfo = @{@"data": data, @"completionHandler": completionHandler}; - [RNFIRMessagingHelper sharedInstance].lastNotificationResponse = userInfo; - + + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + if (data[@"_actionIdentifier"] && ![data[@"_actionIdentifier"] isEqualToString:UNNotificationDefaultActionIdentifier]) { + initialNotificationActionResponse = userInfo; + } + }); + [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:userInfo]; } @@ -325,9 +312,11 @@ RCT_EXPORT_MODULE(); return self; } --(void)startObserving { - if([RNFIRMessagingHelper sharedInstance].lastNotificationResponse) { - [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:[RNFIRMessagingHelper sharedInstance].lastNotificationResponse]; +-(void) addListener:(NSString *)eventName { + [super addListener:eventName]; + + if([eventName isEqualToString:FCMNotificationReceived] && initialNotificationActionResponse) { + [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:[initialNotificationActionResponse copy]]; } } @@ -351,6 +340,8 @@ RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecte } } + + RCT_EXPORT_METHOD(getAPNSToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { NSData * deviceToken = [FIRMessaging messaging].APNSToken; @@ -627,8 +618,10 @@ RCT_EXPORT_METHOD(finishNotificationResponse: (NSString *)completionHandlerId){ } [self sendEventWithName:FCMNotificationReceived body:data]; - - [RNFIRMessagingHelper sharedInstance].lastNotificationResponse = nil; + + if (initialNotificationActionResponse) { + initialNotificationActionResponse = nil; + } } - (void)sendDataMessageFailure:(NSNotification *)notification -- 2.26.2