Ad

Firebase IOS Receive Data From Push Notification

I am currently attempting to send a notification through firebase to iOS which contains data which I would like to save to a variable.

My server is pushing a notification which my phone receives, and the notification contains topic_name, message_body, data_message, sound. All of which work, although I am confused as to how I can access the data. Essentially, I want to save the sent data to a variable.

How would I go about doing this?

Ad

Answer

payload like this

{
    "aps" : {
        "alert" : "Notification with custom payload!",
        "badge" : 1,
        "content-available" : 1
    },
     "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
     }
}

Read the payload data

@available(iOS 10, *)

    extension AppDelegate : UNUserNotificationCenterDelegate {

        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo

              if let aps = userInfo["aps"] as? NSDictionary
             {
                let alert = aps["alert"]as? NSString
                let badge = aps["badge"] as? Int
             }
          completionHandler([.alert, .badge, .sound])

    }
Ad
source: stackoverflow.com
Ad