简体   繁体   中英

How to get data from Firebase FCM push notification in iOS

I am sending out a push notification to:

https://fcm.googleapis.com/fcm/send

formatted as (in php):

   [
      'to' => $token,
      'data' => [
         'key' => $val
      ],
      'notification' => [
         'title' => $title,
         'body' => $message,
         'badge' => $badge,
         'sound' => 'default'
      ]
   ]

And my iOS app receives and displays the push perfectly, both foreground and background. But I can't find how to access data

In my AppDelegate file I have:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

   let aps = userInfo[AnyHashable("aps")] as! JsonObject
   print(aps)
}

and the debug out from the print statement displays:

["sound": default, "badge": 1, "alert": {
    body = "One flew over the cuckoo's nest";
    title = "My Title";
}]

So where is my data: {key:val} ??

The data should be available directly on the userInfo dictionary, not on the "aps" key and you should be able to access it like this:

let customValue = userInfo["customKey"] as? String

As stated on the Firebase documentation ,

The payload of notification messages is a dictionary of keys and values. Notification messages sent through APNs follow the APNs payload format as below:

{
    "aps" : {
      "alert" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
      },
      "badge" : 1,
    },
    "customKey" : "customValue"
  }

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