Firebase iOS receive data from push notification

三世轮回 提交于 2019-12-13 04:43:31

问题


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?


回答1:


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

    }



回答2:


As long as you actually have the data you can pass it around in your code in many ways. One way is to post a notification with the data which the class/object responsible for using the data can listen to. The best solution to use sort of depends on how your app is structured...



来源:https://stackoverflow.com/questions/50325216/firebase-ios-receive-data-from-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!