如何从 IOS swift 中的通知中读取自定义数据

How to read custom data from a notification in IOS swift

我正在构建一个小型 IOS 应用程序,通过 WebKitView 呈现 Web 应用程序。

我需要在 APNS 负载中传递额外的信息来处理应用程序的路由。假设您的 post 有一条新评论。

阅读苹果文档时here。我可以看到可以添加自定义数据。

{
  "aps" : {
    "alert" : "You got your emails.",
    "badge" : 9,
    "sound" : "bingbong.aiff"
  },
  "url" : "https://domain.ext/post/1"
}

如何从以下位置访问 url

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

//        if let page = response.notification["url"] as? String {
//            print(url)
//        }
        completionHandler()
    }

你可以试试

let userInfo = response.notification.request.content.userInfo
print(userInfo["url"])

像这样:

let data = response.notification.request.content.userInfo
if let url = data["url"] as? String {

}