本文介绍了如何获取从iOS 10和Swift 3中的Firebase控制台发送的推送通知的正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个iOS应用程序,该应用程序应接收从Firebase控制台发送的推送通知。根据Firebase文档的建议,我们必须将我们的委托对象分配给 UNUserNotificationCenter c $ c>接收并显示通知的对象,以及 FIRMessaging 对象接收数据消息,然后我们的应用程序完成启动。 这已经在 didFinishLaunchingWithOptions 方法中完成了。现在,当我从Firebase控制台发送消息时,我会通过 applicationReceivedRemoteMessage(_ remoteMessage:FIRMessagingRemoteMessage)方法。 问题是我无法从 remoteMessage.appData 字典。知道正文在 remoteMessage.appData [通知] 内。的确,指令 print(remoteMessage.appData) $ b $ p $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ b body = Hello通知; e = 1; },AnyHashable(from):49679924394,AnyHashable(collapse_key):com.company.app] 打印 remoteMessage.appData [通知] 显示 { body = Hello通知; e = 1; $ / code> 我试过了 remoteMessage.appData [notification] [body] $ b remoteMessage.appData [notification]。body 但是会导致语法错误。我不能提取身体,以显示在警报控制器。 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,*){让authOptions:UNAuthorizationOptions = [.alert,.badge,.sound] let center = UNUserNotificationCenter.current() center.requestAuthorization(options:authOptions,completionHandler:{_,_in}) application.registerForRemoteNotifications() //对于iOS 10显示通知(通过APNS发送) UNUserNotificationCenter.current()。delegate = self //对于iOS 10数据消息通过FCM) FIRMessaging.messaging()。remoteMessageDelegate = self } else {设置:UIUserNotificationSettings = UIUserNotificationSettings(类型:[.alert,.badge,.sound],类别:nil ) application.registerUserNotificationSettings(设置)} application.registerForRemoteNotifications()返回true } //在iOS 10设备上接收数据消息。 func applicationReceivedRemoteMessage(_ remoteMessage:FIRMessagingRemoteMessage){$ b $ print(remoteMessage.appData) print(remoteMessage.appData [notification]!) let alertController = UIAlertController(title: 来自IOBird开发团队的消息,消息:消息的正文,preferredStyle:.alert)让OKAction = UIAlertAction(title:OK,style: .default){(action:UIAlertAction!)in } alertController.addAction(OKAction) self.window?.rootViewController?.present(alertController,animated:true,completion:nil) 解决方案感谢Arthur Thompson的帮助,你给了我这个主意。我发布答案,以防别人需要。我写了 let d:[String:Any] = remoteMessage.appData [notification] as! [String:Any] let body:String = d [body] as!字符串 print(body) 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 instructionprint(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"].bodybut 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)} 解决方案 Thanks Arthur Thompson for your help, you gave me the idea. I post the answer in case someone else need it. I wrotelet d : [String : Any] = remoteMessage.appData["notification"] as! [String : Any]let body : String = d["body"] as! Stringprint(body) 这篇关于如何获取从iOS 10和Swift 3中的Firebase控制台发送的推送通知的正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 10:01