3

I'm using custom payload for notification & I get notification but I'm unable to fetch from Key["data"]. Here is my Payload code

{
    "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"
    },
  }  

I'm trying to access data from didreceive method

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

result is: It's a notification with custom payload! If anyone can help me with this will be appreciated. Thanks in advance Regards.

1 Answer 1

4

Parse custom data in notification's userInfo property instead of body. userInfo is a dictionary of custom information associated with the notification.

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..
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It's working directly when i add this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.