【发布时间】:2016-11-21 21:07:22
【问题描述】:
当我的应用被前景化时,使用带有方法调配的较新 Firebase 云消息传递,我可以成功地在我的应用委托中的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 证书未上传或注册令牌不正确的所有担忧。如果不是这种情况,请发表评论,我会再次进入那个兔子洞。 我可能忽略了一些简单的事情,但是如何在应用程序后台显示通知?谢谢
【问题讨论】:
-
我今天面临着类似的斗争:当应用程序在前台时调用应用程序委托中的处理程序,但当应用程序在后台时没有显示托盘通知。有趣的是看看是否有任何事情发生。如果我弄清楚了,会告诉你的。
-
你看过github上的这个issue了吗?
-
当您的应用程序在前台时,您可以直接从 FCM 接收消息,而不是通过应用程序在后台时使用的 APNs。为了确保 APNs 配置正确,您是否可以通过实现 didRegisterForRemoteNotificationsWithDeviceToken 方法来确认您正在接收 APNs 令牌。
-
@ArthurThompson 在此版本的代码之前,我没有使用方法调配技术,而是实现了 didRegisterForeRemoteNotificationsWIthDeviceToken 方法,我可以在其中获得令牌。起初我希望我已经完成了,但是当这个令牌对 FCM 不起作用时,我不得不重构。这能回答你的问题吗?
-
@Kazimieras 是的,我读了整本书。我尝试将优先级设置为高,内容可用设置为 true。但没有运气。我也尝试同时使用两者。但其他人说那是行不通的,就我而言,这又是真的。
标签: ios swift firebase apple-push-notifications firebase-cloud-messaging