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

爱⌒轻易说出口 提交于 2019-11-29 20:36:36

问题


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 documentation, we must assign our delegate object to the UNUserNotificationCenter object to receive and display notifications, and the FIRMessaging object to receive data messages, before our app finishes launching.

This has been done in the didFinishLaunchingWithOptions method. I followed all the steps in order to configure Firmessaging as well as APNs.

Now, when I send a message from the Firebase console, I receive it through applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) method.

The problem is that I could not extract the body of the message from the remoteMessage.appData dictionary. Knowing that the body is within remoteMessage.appData["notification"]. Indeed, the instruction

print(remoteMessage.appData)

prints

[AnyHashable("notification"): {
   body = Hello Notifications;
   e = 1;
}, AnyHashable("from"): 49679924394, AnyHashable("collapse_key"): com.company.app]

Printing

 remoteMessage.appData["notification"]

shows

{
   body = Hello Notifications;
   e = 1;
}

I tried

remoteMessage.appData["notification"]["body"]

and

 remoteMessage.appData["notification"].body

but it results in syntax error. I could not extract the body in order to show it in an alert controller. The code of the appDelegate is given below.

class AppDelegate: UIResponder, UIApplicationDelegate, FIRMessagingDelegate, UNUserNotificationCenterDelegate{
......

func application(_ application: UIApplication, didFinishLaunchingWithOptions, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.isStatusBarHidden = true
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
    if #available(iOS 10.0, *) {
       let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
       let center = UNUserNotificationCenter.current()
       center.requestAuthorization(options: authOptions, completionHandler: {_ ,_ in })
       application.registerForRemoteNotifications()
// For iOS 10 display notification (sent via APNS)
       UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)        
       FIRMessaging.messaging().remoteMessageDelegate = self         
    } else {
       let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
       application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
     return true
}
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    print(remoteMessage.appData)
    print(remoteMessage.appData["notification"]!)
    let alertController = UIAlertController(title: "Message from IOBird Developer Team", message: "?????? Body of the message ?????", preferredStyle: .alert)
    let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
    }
    alertController.addAction(OKAction)
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}

回答1:


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)



回答2:


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

let body = remoteMessage.appData["notification"]!["body"] as! String
print(body)



回答3:


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




回答4:


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("=====")


来源:https://stackoverflow.com/questions/39883112/how-to-get-the-body-of-a-push-notification-sent-from-firebase-console-in-ios-10

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