How to get the body of a push notification sent from Firebase console in iOS 10 and Swift 3?

后端 未结 4 2015
春和景丽
春和景丽 2021-01-03 00:15

I\'m developing an iOS app that should receive push notifications sent from the Firebase console. I\'m using Swift 3 and iOS 10.

As recommended by the Firebase docum

相关标签:
4条回答
  • 2021-01-03 00:29

    You should be able to retrieve the body using something like:

    let body = remoteMessage.appData["notification"]!["body"] as! String
    print(body)
    
    0 讨论(0)
  • 2021-01-03 00:32

    Thanks Arthur Thompson for your help, you gave me the idea. I post the answer in case someone else need it. I wrote

    let d : [String : Any] = remoteMessage.appData["notification"] as! [String : Any]
    let body : String = d["body"] as! String
    print(body)
    
    0 讨论(0)
  • 2021-01-03 00:32

    You should be able to print any line from the response with this :

    let response = remoteMessage.appData
    
    print(response[AnyHashable("notification")] as Any)
    

    Apple documentation on AnyHashable : https://developer.apple.com/reference/swift/anyhashable

    0 讨论(0)
  • 2021-01-03 00:34

    I had exactly the same problem with Firebase messaging and extracting the data and this works like a charm. (Swift 3)

        print("====")
        let d : [String : Any] = remoteMessage.appData["notification"] as! [String : Any]
        let body : String = d["body"] as! String
        let click_action : String = d["click_action"] as! String
        let title : String = d["title"] as! String
        print(body)
        print(title)
        print(click_action)
        print("=====")
    
    0 讨论(0)
提交回复
热议问题