Skip to content

Commit

Permalink
fix(ios, messaging): register background handler task for FCM events (#…
Browse files Browse the repository at this point in the history
…4180)

* Minor fix to handle notifications in background state, now we are registering background task

* Apply suggestions from code review

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
davx1992 and mikehardy authored Aug 31, 2020
1 parent 56bb657 commit cf706c6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,22 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N

if (userInfo[@"gcm.message_id"]) {
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
// If app is in background state, register background task to guarantee async queues aren't frozen.
UIBackgroundTaskIdentifier __block backgroundTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
if (backgroundTaskId != UIBackgroundTaskInvalid) {
[application endBackgroundTask:backgroundTaskId];
backgroundTaskId = UIBackgroundTaskInvalid;
}
}];
// TODO add support in a later version for calling completion handler directly from JS when user JS code complete
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (25 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
completionHandler(UIBackgroundFetchResultNewData);

// Stop background task after the longest timeout, async queue is okay to freeze again after handling period
if (backgroundTaskId != UIBackgroundTaskInvalid) {
[application endBackgroundTask:backgroundTaskId];
backgroundTaskId = UIBackgroundTaskInvalid;
}
});

// TODO investigate later - RN bridge gets invalidated at start when in background and a new bridge created - losing all events
Expand Down

1 comment on commit cf706c6

@vercel
Copy link

@vercel vercel bot commented on cf706c6 Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.