@@ -156,30 +156,34 @@ - (void)application:(UIApplication *)application
156156 DLog (@" didReceiveRemoteNotification gcm.message_id was present %@ " , userInfo);
157157
158158 if ([UIApplication sharedApplication ].applicationState == UIApplicationStateBackground) {
159+ // Store the completion handler to call later when the JS code finishes
160+ RNFBMessagingAppDelegate *sharedInstance = [RNFBMessagingAppDelegate sharedInstance ];
161+ sharedInstance.completionHandler = completionHandler;
162+
159163 // If app is in background state, register background task to guarantee async queues aren't
160164 // frozen.
161- UIBackgroundTaskIdentifier __block backgroundTaskId =
162- [application beginBackgroundTaskWithExpirationHandler: ^{
163- if (backgroundTaskId != UIBackgroundTaskInvalid) {
164- [application endBackgroundTask: backgroundTaskId];
165- backgroundTaskId = UIBackgroundTaskInvalid;
166- }
167- }];
168- // TODO add support in a later version for calling completion handler directly from JS when
169- // user JS code complete
165+ sharedInstance.backgroundTaskId = [application beginBackgroundTaskWithExpirationHandler: ^{
166+ if (backgroundTaskId != UIBackgroundTaskInvalid) {
167+ [application endBackgroundTask: backgroundTaskId];
168+ backgroundTaskId = UIBackgroundTaskInvalid;
169+ }
170+ }];
171+
170172 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(25 * NSEC_PER_SEC)),
171173 dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0 ), ^{
172- completionHandler (UIBackgroundFetchResultNewData);
174+ if (sharedInstance.completionHandler ) {
175+ sharedInstance.completionHandler (UIBackgroundFetchResultNewData);
176+ sharedInstance.completionHandler = nil ;
177+ }
173178
174179 // Stop background task after the longest timeout, async queue is okay to
175180 // freeze again after handling period
176- if (backgroundTaskId != UIBackgroundTaskInvalid) {
177- [application endBackgroundTask: backgroundTaskId];
178- backgroundTaskId = UIBackgroundTaskInvalid;
181+ if (sharedInstance. backgroundTaskId != UIBackgroundTaskInvalid) {
182+ [application endBackgroundTask: sharedInstance. backgroundTaskId];
183+ sharedInstance. backgroundTaskId = UIBackgroundTaskInvalid;
179184 }
180185 });
181186
182- RNFBMessagingAppDelegate *sharedInstance = [RNFBMessagingAppDelegate sharedInstance ];
183187 [sharedInstance.conditionBackgroundMessageHandlerSet lock ];
184188 @try {
185189 DLog (@" didReceiveRemoteNotification sharedInstance.backgroundMessageHandlerSet = %@ " ,
0 commit comments