Home  >  Article  >  Backend Development  >  Why Are Firebase Cloud Messaging (FCM) Notifications Not Retained in Background Mode on iOS?

Why Are Firebase Cloud Messaging (FCM) Notifications Not Retained in Background Mode on iOS?

Susan Sarandon
Susan SarandonOriginal
2024-10-20 15:22:02926browse

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:

  • Incorrect PHP code for sending notifications.
  • Lack of background mode support in the iOS application.

Solution:

PHP Code:

  • Add the following fields to your PHP payload:

    • 'content_available' => true (to trigger when iOS app is in the background)
    • 'priority' => 'high'
    • 'notification' => $data (replace $data with your desired notification data)
  • Set $data to include both 'message' and 'body' keys with custom values.

iOS Application:

  1. Check if the .gcm file is included in your Xcode project.
  2. Ensure that AppDelegate conforms to GCMReceiverDelegate.
  3. Override the applicationDidBecomeActive and applicationDidEnterBackground methods to (re)connect and disconnect from FCM, respectively.
  4. Register for notifications using UIRemoteNotificationType.
  5. Implement the application:didReceiveRemoteNotification: method to handle received notifications, even when the app is in the background.
  6. If needed, implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method to further handle notifications received in the background.

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:

  • Ensure that you have enabled push notifications for your app in the Apple Developer Portal.
  • Add the push notification certificate in your FCM project settings.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn