Firebase iOS 从推送通知接收数据
Firebase iOS receive data from push notification
我目前正在尝试通过 firebase 向 iOS 发送通知,其中包含我想保存到变量的数据。
我的服务器正在推送我的 phone 收到的通知,通知包含 topic_name, message_body, data_message, sound
。所有这些都有效,尽管我对如何访问数据感到困惑。本质上,我想将发送的数据保存到一个变量中。
我该怎么做?
只要您确实拥有数据,就可以通过多种方式在代码中传递它。一种方法是 post 一个带有数据的通知,负责使用数据的 class/object 可以收听。使用哪种最佳解决方案取决于您的应用程序的结构...
像这样的负载
{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}
读取负载数据
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])
}
我目前正在尝试通过 firebase 向 iOS 发送通知,其中包含我想保存到变量的数据。
我的服务器正在推送我的 phone 收到的通知,通知包含 topic_name, message_body, data_message, sound
。所有这些都有效,尽管我对如何访问数据感到困惑。本质上,我想将发送的数据保存到一个变量中。
我该怎么做?
只要您确实拥有数据,就可以通过多种方式在代码中传递它。一种方法是 post 一个带有数据的通知,负责使用数据的 class/object 可以收听。使用哪种最佳解决方案取决于您的应用程序的结构...
像这样的负载
{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}
读取负载数据
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])
}