如何在 iOS 10 和 Swift 3 中获取从 Firebase 控制台发送的推送通知的 body?
How to get the body of a push notification sent from Firebase console in iOS 10 and Swift 3?
我正在开发一个 iOS 应用程序,它应该接收从 Firebase 控制台发送的推送通知。我正在使用 Swift 3 和 iOS 10。
根据 Firebase 文档的建议,我们必须将委托 object 分配给 UNUserNotificationCenter
object 以接收和显示通知,以及 FIRMessaging
object 在我们的应用程序完成启动之前接收数据消息。
这已在 didFinishLaunchingWithOptions
方法中完成。我按照所有步骤来配置 Firmessaging 和 APNs。
现在,当我从 Firebase 控制台发送消息时,我会通过 applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage)
方法接收它。
问题是我无法从 remoteMessage.appData
字典中提取消息的 body。知道 body 在 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
但它会导致语法错误。我无法提取 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)
}
您应该能够使用类似以下的方法检索正文:
let body = remoteMessage.appData["notification"]!["body"] as! String
print(body)
感谢 Arthur Thompson 的帮助,您给了我灵感。我 post 的答案以防其他人需要它。我写了
let d : [String : Any] = remoteMessage.appData["notification"] as! [String : Any]
let body : String = d["body"] as! String
print(body)
您应该能够打印响应中的任何行:
let response = remoteMessage.appData
print(response[AnyHashable("notification")] as Any)
关于 AnyHashable 的 Apple 文档:https://developer.apple.com/reference/swift/anyhashable
我在 Firebase 消息传递和提取数据方面遇到了完全相同的问题,这非常有效。 (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("=====")
最好的方法
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
let apsData = (userInfo["order_id"] as? NSString)?.integerValue
let d : [String : Any] = userInfo["aps"] as! [String : Any]
let nh : [String : Any] = d["alert"] as! [String : Any]
let body : String = nh["body"] as! String
let title : String = nh["title"] as! String
print(body,title)
// if you need to go some views
if apsData != nil {
let sb = UIStoryboard(name: "Order", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "RequestDeliveriesVC") as! RequestDeliveriesVC
vc.order_ofer = apsData ?? 0
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let window = appDelegate.window else {return}
window.rootViewController = vc
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: nil, completion: nil)
} else {
}
print(userInfo)
completionHandler()
}
}
我正在开发一个 iOS 应用程序,它应该接收从 Firebase 控制台发送的推送通知。我正在使用 Swift 3 和 iOS 10。
根据 Firebase 文档的建议,我们必须将委托 object 分配给 UNUserNotificationCenter
object 以接收和显示通知,以及 FIRMessaging
object 在我们的应用程序完成启动之前接收数据消息。
这已在 didFinishLaunchingWithOptions
方法中完成。我按照所有步骤来配置 Firmessaging 和 APNs。
现在,当我从 Firebase 控制台发送消息时,我会通过 applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage)
方法接收它。
问题是我无法从 remoteMessage.appData
字典中提取消息的 body。知道 body 在 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
但它会导致语法错误。我无法提取 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)
}
您应该能够使用类似以下的方法检索正文:
let body = remoteMessage.appData["notification"]!["body"] as! String
print(body)
感谢 Arthur Thompson 的帮助,您给了我灵感。我 post 的答案以防其他人需要它。我写了
let d : [String : Any] = remoteMessage.appData["notification"] as! [String : Any]
let body : String = d["body"] as! String
print(body)
您应该能够打印响应中的任何行:
let response = remoteMessage.appData
print(response[AnyHashable("notification")] as Any)
关于 AnyHashable 的 Apple 文档:https://developer.apple.com/reference/swift/anyhashable
我在 Firebase 消息传递和提取数据方面遇到了完全相同的问题,这非常有效。 (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("=====")
最好的方法
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
let apsData = (userInfo["order_id"] as? NSString)?.integerValue
let d : [String : Any] = userInfo["aps"] as! [String : Any]
let nh : [String : Any] = d["alert"] as! [String : Any]
let body : String = nh["body"] as! String
let title : String = nh["title"] as! String
print(body,title)
// if you need to go some views
if apsData != nil {
let sb = UIStoryboard(name: "Order", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "RequestDeliveriesVC") as! RequestDeliveriesVC
vc.order_ofer = apsData ?? 0
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let window = appDelegate.window else {return}
window.rootViewController = vc
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: nil, completion: nil)
} else {
}
print(userInfo)
completionHandler()
}
}