从Custom Payload Swift获取数据

问题描述 投票:0回答:1

我正在使用自定义有效负载进行通知,但收到通知,但无法从Key [“ data”]中获取。这是我的有效载荷代码

{
    "Simulator Target Bundle": "com.xyz.zyxapp",
   "aps" : {
        "alert" : "It's a notification with custom payload!",
        "badge" : 1,
        "content-available" : 0         
    },
    "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
    },
  }  

我正在尝试从didreceive方法访问数据

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
  {
     print(response.notification.request.content.body)
}

结果是:这是带有自定义有效负载的通知!如果有人可以帮助我,将不胜感激。提前致谢问候。

ios swift notifications firebase-notifications
1个回答
0
投票

解析通知的userInfo属性中的自定义数据,而不是正文。 userInfo是与通知关联的自定义信息的词典。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let data = userInfo["data"] as? [String: Any] {
        //
        //Do your parsing here..
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.