gpt4 book ai didi

ios - 为什么在Swift5中无法读取推送消息数据?

转载 作者:行者123 更新时间:2023-11-28 13:27:29 24 4
gpt4 key购买 nike

我有一个问题,我此时无法读取推送消息数据。我正常收到消息。

当我按下主页按钮时,当应用程序处于前台或后台时,我可以同时收到。

但是我在日志中看不到消息数据。

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 中发送测试消息

enter image description here

除了应用程序的 application 功能外,我还有两个 messaging 功能。我不需要这些消息功能吗?这些函数从未向我显示日志。

最佳答案

您正在使用的方法在 iOS 10 中已弃用。

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application

从 iOS 10 开始,通知框架用更新的方法替换了这个方法:

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)

通知框架的更多详细信息可用here

关于ios - 为什么在Swift5中无法读取推送消息数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com