简体   繁体   中英

How to read custom data from a notification in IOS swift

I'm building a small IOS application rendering a web application via WebKitView.

I need to pass an extra information in the APNS payload to handle the routing of the app. Let's say your post has a new comment.

When reading the apple documentation here . I can see custom data can be added.

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

How can I access url from:

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

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

You can try

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

like this:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM