如何从iOS 10和Swift 3中的Firebase控制台发送推送通知的正文?

时间:2016-10-05 20:28:56

标签: ios firebase swift3 ios10 firebase-notifications

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

根据Firebase文档的建议,我们必须在我们的应用程序完成启动之前,将我们的委托对象分配给UNUserNotificationCenter对象以接收和显示通知,并将FIRMessaging对象分配给接收数据消息。< / p>

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

现在,当我从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

但会导致语法错误。我无法提取身体以便在警报控制器中显示它。 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)
}

4 个答案:

答案 0 :(得分:5)

感谢Arthur Thompson的帮助,你给了我这个主意。我发布答案以防其他人需要它。我写了

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

答案 1 :(得分:0)

您应该能够使用以下内容检索身体:

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

答案 2 :(得分:0)

您应该可以打印响应中的任何行:

let response = remoteMessage.appData

print(response[AnyHashable("notification")] as Any)

关于AnyHashable的Apple文档:https://developer.apple.com/reference/swift/anyhashable

答案 3 :(得分:0)

我遇到了与Firebase消息传递完全相同的问题并提取数据,这就像魅力一样。 (斯威夫特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("=====")