【发布时间】:2019-09-26 06:09:14
【问题描述】:
我有一个问题,我此时无法读取推送消息数据。我正常收到消息。
当我按下主页按钮时,我可以在应用程序处于前台或后台时同时接收。
但是我在日志中看不到消息数据。
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
...
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
guard
let aps = data[AnyHashable("notification")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String,
let title = alert["title"] as? String
else {
// handle any error here
return
}
Log.Info("Title: \(title) \nBody:\(body)")
Messaging.messaging().appDidReceiveMessage(data)
// Print full message.
Log.Info(data)
}
...
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
Log.Info("fcmToken \(fcmToken)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
Log.Info("remort \(remoteMessage.appData)")
}
我发送的数据
{
notification : {
"title" : "test title.",
"body" : "test context."
},
data : {
"image" : "http://11.111.111.111:100000000/_img/sample_01.jpg",
"page_url" : "http://11.111.111.111:100000000/Point?address=",
"type" : "point"
}
}
无法在任何功能中查看日志。我错过了什么?请让我知道出了什么问题。
编辑
我修改了错误的数据并更改了日志的位置。但我没有任何日志。
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Print full message.
Log.Info(data)
guard
let aps = data[AnyHashable("notification")] as? NSDictionary,
let body = aps["body"] as? String,
let title = aps["title"] as? String
else {
// handle any error here
return
}
Log.Info("Title: \(title) \nBody:\(body)")
Messaging.messaging().appDidReceiveMessage(data)
}
在 firebase 中发送测试消息
除了应用程序的application 函数外,我还有两个messaging 函数。我不需要这些消息功能吗?这些函数从未向我显示过日志。
【问题讨论】:
-
我在您的数据中没有看到关键的“警报”,因此使用了 swift 字典而不是 NSDictionary
-
嗨@JoakimDanielson 没错,但我什至看不到完整数据。 'Log.Info(数据)'
-
将该日志语句移到函数的开头,如果保护语句失败,它将退出,其余的不执行
-
@JoakimDanielson 请看我修改的问题。
-
@JoakimDanielson 除了应用程序功能外,我还有两个消息功能。这没有必要吗?
标签: ios swift firebase push-notification