gpt4 book ai didi

ios - 如何使用 Firebase 读取推送通知中发送的自定义数据

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:35 24 4
gpt4 key购买 nike

我正在尝试检索我在 iOS 应用程序 (Swift) 中使用 Firebase 在推送通知中发送的“数据”

这是我用来发送通知的代码:

{
"to": "dWB537Nz1GA:APA91bHIjJ5....",
"data":
{
"message": "Offer!",
"mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
},
"notification":
{
"body": "Enter your message",
"sound": "default"
}
}

我只实现了一种从通知中读取数据的方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//checked where I can get custom data which I sent but no success
let notification = response.notification.request.content.body

print("Notification recived content:\(response.notification.request.content)")
print("userNotificationCenter : didReceive response body : \(notification)")
print("userNotificationCenter : didReceive response user info : \(response.notification.request.content.userInfo)")
print("userNotificationCenter : response : \(response)")


completionHandler()
}

检查上面的方法,我可以在其中获取我发送但没有成功的自定义数据

我在很多地方搜索解决方案但没有成功。请帮助我检索自定义数据。

提前致谢!

最佳答案

我在 swift 方面是这样使用的:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


if let menu = userInfo["Menu"] as? NSString {
print(menu)
}

Messaging.messaging().appDidReceiveMessage(userInfo)

completionHandler(UIBackgroundFetchResult.newData)
}

这是我用来发送通知的代码:

{
"to": "/topics/....",
"data":
{
"Menu": "Summary"
},
"notification":
{
"title": "Header",
"text": "Message",
"sound": "default"
}
}

关于ios - 如何使用 Firebase 读取推送通知中发送的自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498363/

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