Firebase Cloud Messaging iOS:无后台推送通知

使用更新的Firebase云消息传递方法,我可以在应用程序的didReceiveRemoteNotification在应用程序委托中的didReceiveRemoteNotification方法中成功接收有效内容。 但是,我没有得到任何有效负载,而且当我的应用程序是后台时, didReceiveRemoteNotification不会被调用,尽pipe消息成功发送的API响应(见下文)
以下是我发送到FCM API以触发推送通知的请求https://fcm.googleapis.com/fcm/send

 { "to": "{valid-registration-token}", "priority":"high", //others suggested setting priority to high would fix, but did not "notification":{ "body":"Body3", "title":"test" } } 

与FCM的回应

 { "multicast_id": 6616533575211076304, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:1468906545447775%a4aa0efda4aa0efd" } ] } 

这是我的appDelegate代码

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { Fabric.with([Crashlytics.self]) FIRApp.configure() NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil) return true } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { // Let FCM know about the message for analytics etc. FIRMessaging.messaging().appDidReceiveMessage(userInfo) // Print full message. print("%@", userInfo) // handle your message // let localNotification = UILocalNotification() // localNotification.fireDate = NSDate() // let notification = userInfo["notification"]! // localNotification.alertBody = notification["body"] as? String // localNotification.alertTitle = notification["title"] as? String // localNotification.timeZone = NSTimeZone() // application.scheduleLocalNotification(localNotification) } func tokenRefreshNotification(notification: NSNotification) { let refreshedToken = FIRInstanceID.instanceID().token()! print("InstanceID token: \(refreshedToken)") LoginVC.registerForPushNotifications() connectToFcm() } //foreground messages func applicationDidBecomeActive(application: UIApplication) { connectToFcm() } // [START disconnect_from_fcm] func applicationDidEnterBackground(application: UIApplication) { FIRMessaging.messaging().disconnect() print("Disconnected from FCM.") } func connectToFcm() { FIRMessaging.messaging().connectWithCompletion { (error) in if (error != nil) { print("Unable to connect with FCM. \(error)") } else { print("Connected to FCM.") } } } } 

我稍后在我的应用程序中调用此代码来请求权限

 static func registerForPushNotifications() { let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() } 

由于我能够收到通知,而应用程序是foregrounded我假设这将消除所有的担心,我的apns证书没有上传或注册令牌是不正确的。 如果不是这样的话,请评论一下,我会再次下到那个兔子洞。 有可能是简单的,我俯瞰,但我怎么能得到的通知,而应用程序是背景时出现? 谢谢

显然,(尽pipe使用方法调整应该自动执行),但您仍然必须在Firebase实例上设置APNS令牌。 因此,这意味着您还必须使用类似的方法来实现didRegisterForRemoteNOtificationsWithDeviceToken方法

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { // set firebase apns token FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown) } 

作为补充说明。 使用"priority":"high"对testing是有用的,因为它立即发送通知。

确保在JSON负载中添加了“content_available”:true。 否则,您将无法在后台模式下收到推送通知。

 "notification" : { "content_available" : true, "body" : "this is body", "title" : "this is title" } 

AFAIK,对于ios,你将无法使用FCM发送推送通知消息的应用程序在后台或死亡状态。

Firebase推送通知