Home >Backend Development >PHP Tutorial >Why Are Firebase Cloud Messaging (FCM) Notifications Not Retained in Background Mode on iOS?
Firebase Cloud Messaging (FCM): Notifications Not Received When App Is in Background Mode on iOS
FCM allows developers to send push notifications to iOS and Android devices. This issue occurs when notifications are not received when the app is in the background.
Possible Causes:
Solution:
PHP Code:
Add the following fields to your PHP payload:
iOS Application:
Updated PHP Code:
<code class="php"><?php $data = array( 'message' => 'Hello World!', 'body' => 'Hello World!' ); $post = array( 'registration_ids' => $ids, 'data' => $data, 'content_available' => true, 'priority' => 'high', 'notification' => $data ); // ... Remaining code</code>
Updated iOS Application:
<code class="objc">// AppDelegate.h @interface AppDelegate : UIResponder <UIApplicationDelegate, GCMReceiverDelegate> // AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... // Register for remote notifications if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { UIRemoteNotificationType allNotificationTypes = (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge); [application registerForRemoteNotificationTypes:allNotificationTypes]; } else { UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } // ... } - (void)applicationDidEnterBackground:(UIApplication *)application { [[GCMService sharedInstance] disconnect]; _connectedToGCM = NO; } - (void)applicationDidBecomeActive:(UIApplication *)application { [[GCMService sharedInstance] connectWithHandler:^(NSError *error) { // ... }]; } // ...</code>
Note:
The above is the detailed content of Why Are Firebase Cloud Messaging (FCM) Notifications Not Retained in Background Mode on iOS?. For more information, please follow other related articles on the PHP Chinese website!