Firebase 云消息传递 (FCM):当应用程序在 iOS 上处于后台模式时未收到通知
FCM 允许开发者将推送通知发送到iOS 和 Android 设备。当应用程序在后台时未收到通知时,就会出现此问题。
可能原因:
解决方案:
PHP 代码:
将以下字段添加到您的 PHP 负载中:
iOS 应用程序:
更新的 PHP 代码:
<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>
更新的 iOS 应用程序:
<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>
注意:
以上是为什么 Firebase 云消息传递 (FCM) 通知在 iOS 上的后台模式下不保留?的详细内容。更多信息请关注PHP中文网其他相关文章!