我是iOS的新手,目前正在开发约会应用程序。我已经在Android中实现了firebase push
,并且工作正常,但是在iOS设备上收到通知时,通知的格式为原始json
。
在搜索时,我知道iOS正在为推送通知提供固定格式,例如:
{
"aps": {
"alert":"Testing.. (0)",
"badge":1,
"sound":"default",
"mutable-content": 1,
"attachment-url": ""
}
}
但是我的json
数据采用其他格式,因此我无法解析该数据以显示适当的通知。
My Format
{
"aps" : {
"badge" : 50,
"alert" : {
"body" : "{\"notificationId\":\"Flootapp\",\"DateMatchHistoryId\":\"BJgV-_PYX\",\"id\":\"Syn-XlnHX\",\"message\":\"Congratulations! You have a match with Rishi Raj. Confirm quickly\",\"type\":\"matched\"}",
"title" : "Floot"
}
},
"gcm.message_id" : "0:1537863976816788%835a2461835a2461",
"message" : "{\"notificationId\":\"Flootapp\",\"DateMatchHistoryId\":\"BJgV-_PYX\",\"id\":\"Syn-XlnHX\",\"message\":\"Congratulations! You have a match with Rishi Raj. Confirm quickly\",\"type\":\"matched\"}",
"badge" : "50",
"messageFrom" : "Floot",
"alert" : "{\"notificationId\":\"Flootapp\",\"DateMatchHistoryId\":\"BJgV-_PYX\",\"id\":\"Syn-XlnHX\",\"message\":\"Congratulations! You have a match with Rishi Raj. Confirm quickly\",\"type\":\"matched\"}",
"google.c.a.e" : "1"
}
我在这里没有任何东西。请帮忙。 预先感谢!
答案 0 :(得分:1)
我认为这会对您有所帮助。 首先,您需要从推送通知响应中找到用户信息。
您需要在此“ YourKey”中设置要访问的密钥。您将获得[String: Any]
类型的对象。之后,您可以从dict
对象访问您的值。
如果获取message
数据,则需要设置message
代替YourKey
。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
let info = response.notification.request.content.userInfo
if let notiStr = info["YourKey"] as? String, let dict = self.convertToDictionary(text: notiStr) {
print(items: dict)
}
return completionHandler()
}
func convertToJson(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
答案 1 :(得分:0)
希望这对您有帮助。在appdelegate中使用它:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print(userInfo)
let aps = userInfo[AnyHashable("aps")] as? NSDictionary
print(aps!)
let alert = aps!["alert"] as? NSDictionary
print(alert!)
let body = let body = alert![AnyHashable("body")] as? String
let title = alert!["title"] as? String
print(title!)
print(body!)
let jsonData = body?.data(using: .utf8)!
let json = try! JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments) as! NSDictionary
print(json)
let notificationId = json["notificationId"] as! String
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: true)
let content = UNMutableNotificationContent()
content.title = "YourAppName"
content.body = userInfo.description
let request = UNNotificationRequest(identifier: "YourAppName", content: content, trigger: trigger)
let currentBadgeNumber = UIApplication.shared.applicationIconBadgeNumber
let updatedBadgeNumber = currentBadgeNumber + 1
if (updatedBadgeNumber > -1) { UIApplication.shared.applicationIconBadgeNumber = updatedBadgeNumber }
UNUserNotificationCenter.current().add(request){ (error) in
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let content = UNMutableNotificationContent()
content.title = "YourAppName"
guard
let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let _ = alert["body"] as? String,
let _ = alert["title"] as? String
else {
return
}
content.title = (alert["title"] as? String)!
content.body = (alert["body"] as? String)!
notificationText = ""
let request = UNNotificationRequest(identifier: "YourAppName", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request){ (error) in
}
self.backGround = "true"
}