Skip to content

Commit

Permalink
fix(messaging, ios): reject notification registration after 10 seconds
Browse files Browse the repository at this point in the history
Previously there were cases where notification registration requests would
wait indefinitely for the choreography of "request registration from system",
"wait for system to respond" completed.

There are cases (specifically if you are missing permissions...) where this
choreography will never complete.

Now we have a delayed async task that waits (for 10 seconds currently) and
if it runs and sees that the choreography wasn't complete it cleans up and
rejects so you avoid infinite hangs

See #7272
  • Loading branch information
mikehardy committed May 17, 2024
1 parent 56b9ad4 commit bb49acb
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ - (NSDictionary *)constantsToExport {
: (RCTPromiseRejectBlock)reject) {
#if TARGET_IPHONE_SIMULATOR
#if !TARGET_CPU_ARM64
// Do the registration on this unsupported simulator, but don't set up to wait for a token that
// won't arrive
// Register on this unsupported simulator, but no waiting for a token that won't arrive
[[UIApplication sharedApplication] registerForRemoteNotifications];
resolve(@([RCTConvert BOOL:@(YES)]));
return;
Expand All @@ -317,6 +316,7 @@ - (NSDictionary *)constantsToExport {
if (@available(iOS 10.0, *)) {
#pragma pop
if ([UIApplication sharedApplication].isRegisteredForRemoteNotifications == YES) {
DLog(@"RNFBMessaging registerForRemoteNotifications - already registered.");
resolve(@([RCTConvert BOOL:@(YES)]));
return;
} else {
Expand All @@ -326,6 +326,32 @@ - (NSDictionary *)constantsToExport {
// Apple docs recommend that registerForRemoteNotifications is always called on app start
// regardless of current status
dispatch_async(dispatch_get_main_queue(), ^{
// Sometimes the registration never completes, which deserves separate attention in other
// areas. This area should protect itself against hanging forever regardless. Just in case,
// check in after a delay and cleanup if required
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
if ([RNFBMessagingAppDelegate sharedInstance].registerPromiseResolver != nil) {
// if we got here and resolve/reject are still set, unset, log failure, reject
DLog(@"RNFBMessaging dispatch_after block: we appear to have timed out. Rejecting");
[[RNFBMessagingAppDelegate sharedInstance] setPromiseResolve:nil
andPromiseReject:nil];

[RNFBSharedUtils
rejectPromiseWithUserInfo:reject
userInfo:[@{
@"code" : @"unknown-error",
@"message" :
@"registerDeviceForRemoteMessages requested but "
@"system did not respond. Possibly missing permission."
} mutableCopy]];
return;
} else {
DLog(@"RNFBMessaging dispatch_after: registerDeviceForRemoteMessages handled.");
return;
}
});

[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
Expand Down

0 comments on commit bb49acb

Please sign in to comment.