David
David

Reputation: 3318

Access custom push notification payload

I'm working with Xcode 9.4.1 (9F2000) and Swift 3.

I'm using these data for push notification:

{
    "aps":{
        "alert":{
            "title":"Hello",
            "body":"How are you?"
            },
            "badge":0,
            "sound":"default",
            "my_value":"This is a test!"
    }
}

This is the function I have:

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

    let action = response.actionIdentifier
    let request = response.notification.request
    let content = request.content

    print("payload data: \(action)\(request)\(content)\n")

    completionHandler()
}

I thought the custom payload should be in request or content. But when doing print("payload data: \(action)\(request)\(content)\n"), it doesn't appear.

What am I doing wrong?

Upvotes: 3

Views: 4623

Answers (1)

MarkWarriors
MarkWarriors

Reputation: 554

You can take the dictionary with

let content = response.notification.request.content.userInfo
if let aps = content["aps"] as? [String: AnyObject] {
    let myValue = aps["my_value"]
    // DO STUFF, in myValue you will find your custom data
}

Upvotes: 10

Related Questions