Firebase iOS 从推送通知接收数据

Swi*_*eek 1 apple-push-notifications firebase swift

我目前正在尝试通过 firebase 向 iOS 发送通知,其中包含我想保存到变量的数据。

我的服务器正在推送我的手机收到的通知,该通知包含topic_name, message_body, data_message, sound. 所有这些都有效,尽管我对如何访问数据感到困惑。本质上,我想将发送的数据保存到一个变量中。

我该怎么做呢?

nag*_*aga 5

像这样的有效载荷

{
    "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"
     }
}
Run Code Online (Sandbox Code Playgroud)

读取有效载荷数据

@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])

    }
Run Code Online (Sandbox Code Playgroud)