ios - 如何获取从 iOS 10 和 Swift 3 中的 Firebase 控制台发送的推送通知的主体?

标签 ios firebase swift3 ios10 firebase-notifications

我正在开发一个 iOS 应用程序,它应该接收从 Firebase 控制台发送的推送通知。我使用的是 Swift 3 和 iOS 10。

按照 Firebase 文档的建议,我们必须将委托(delegate)对象分配给 UNUserNotificationCenter 对象以接收和显示通知,并将 FIRMessaging 对象分配给接收数据消息,然后我们的应用程序完成启动。

这已在 didFinishLaunchingWithOptions 方法中完成。我按照所有步骤来配置 Firmessaging 和 APNs。

现在,当我从 Firebase 控制台发送消息时,我通过 applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) 方法接收它。

问题是我无法从 remoteMessage.appData 字典中提取消息正文。知道正文在 remoteMessage.appData["notification"] 中。的确,指令

print(remoteMessage.appData)

打印

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

打印

 remoteMessage.appData["notification"]

表演

{
   body = Hello Notifications;
   e = 1;
}

我试过了

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

 remoteMessage.appData["notification"].body

但它会导致语法错误。我无法提取主体以便在警报 Controller 中显示它。 appDelegate 的代码如下。

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

最佳答案

感谢亚瑟·汤普森 (Arthur Thompson) 的帮助,您给了我灵感。我发布答案以防其他人需要它。我写了

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

关于ios - 如何获取从 iOS 10 和 Swift 3 中的 Firebase 控制台发送的推送通知的主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39883112/

相关文章:

swift - Firebase:观察 childAdded 返回现有/旧记录?

javascript - Firebase 身份验证、phonegap 构建和 deviceready 事件

swift - JSQMessageAvatarImageFactory错误

ios - Swift - 如何计算字节数并将它们转换为兆字节?

iPhone开发者计划;将个人订阅添加到团队帐户?

javascript - 在 UIWebView 中加载一个 javascript 文件

按下按钮时,IOS Swift 完成功能

ios - 无法完成 dSYM 的提交,文件不再存在于 (null)

ios - 我如何像引用 IBOutlet 一样在代码中引用 gamescene.sks 中添加的 Sprite ?

ios - 由于异步,在发布后连接到 MQTT