当我的应用前景化时,使用带有方法调配的较新的 Firebase 云消息传递,我能够在我的应用委托(delegate)中的 didReceiveRemoteNotification
方法中成功接收有效负载。然而,当我的应用程序处于后台时,我没有得到任何类型的有效负载并且 didReceiveRemoteNotification
没有被调用,尽管 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()
}
由于我能够在应用程序处于前台状态时收到通知,我认为这将消除所有关于我的 apns 证书未上传或注册 token 不正确的担忧。如果不是这种情况,请发表评论,我会再次进入那个兔子洞。 我可能忽略了一些简单的事情,但是如何在应用程序后台运行时显示通知?谢谢
最佳答案
显然,(尽管使用应该自动执行的方法调配)您仍然必须在 Firebase 实例上设置 APNS token 。因此,这意味着您还必须使用类似
的方法实现didRegisterForRemoteNOtificationsWithDeviceToken
方法
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// set firebase apns token
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
}
作为补充说明。使用 "priority":"high"
对测试很有用,因为它会立即发送通知。
关于ios - Firebase 云消息传递 iOS : No background push notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450595/