gpt4 book ai didi

swift - Firebase iOS 从推送通知接收数据

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

我目前正在尝试通过 firebase 向 iOS 发送通知,其中包含我想保存到变量中的数据。

我的服务器正在推送我的手机收到的通知,通知包含topic_name, message_body, data_message, sound。所有这些都有效,尽管我对如何访问数据感到困惑。本质上,我想将发送的数据保存到一个变量中。

我该怎么做?

最佳答案

像这样的负载

{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}

读取负载数据

@available(iOS 10, *)

extension AppDelegate : UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo

if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])

}

关于swift - Firebase iOS 从推送通知接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50325216/

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