Adding delay to avoid notification alert getting lost in iOS

Sometimes, in iOS devices, if the notification opens when the application is not running or is in the background, then the application cannot read the notification payload, which results in an alert view not getting displayed.

It happens because the application takes more time to load all the libraries and in meantime JS tries to display the alert.

Normally, when the application runs in the foreground or background, the notification alert does not get lost as the application is fully loaded prior to notification.

To display the notification alert, add wait/delay in didFinishLaunchingWithOptions so that the alert view of the notification gets displayed.

use the following snippet to add inside the AppDelegate.m class under the didFinishLaunchingWithOptions method:

if (launchOptions != nil) {
     NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if(userInfo.allKeys.count > 0) {
       // You can add any dispatch delay
		dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          [[NSNotificationCenter defaultCenter] postNotificationName:@"CDVMFPPushDidReceiveRemoteNotification" object:userInfo];     
            });
        }
   }
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on March 12, 2021