首页 > 解决方案 > 如何从 IOS swift 中的通知中读取自定义数据

问题描述

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

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

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

{
  "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()
    }

标签: iosswiftapple-push-notifications

解决方案


你可以试试

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

推荐阅读