iOS:无法解析来自firebase的推送通知

iOS:无法解析来自firebase的推送通知,ios,swift,firebase,push-notification,firebase-cloud-messaging,Ios,Swift,Firebase,Push Notification,Firebase Cloud Messaging,我是iOS新手,目前正在开发一款约会应用程序。我已经在安卓系统中实现了firebase推送,它工作正常,但当iOS设备上出现通知时,它将以原始json格式出现 在搜索时,我了解到iOS为推送通知提供了固定格式,如: { "aps": { "alert":"Testing.. (0)", "badge":1, "sound":"default", "mutable-content": 1, "attac

我是iOS新手,目前正在开发一款约会应用程序。我已经在安卓系统中实现了
firebase推送
,它工作正常,但当iOS设备上出现通知时,它将以原始
json
格式出现

在搜索时,我了解到iOS为推送通知提供了固定格式,如:

{ 
  "aps": {
         "alert":"Testing.. (0)",
         "badge":1,
         "sound":"default",
         "mutable-content": 1,
         "attachment-url": ""
         }
}
但是我的
json
数据以其他格式出现,我无法解析它以显示正确的通知

我的格式

{
 "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"

 }
我在这里遗漏了什么。请帮忙。
提前谢谢

我想这对你有帮助。 首先,您需要从推送通知响应中查找用户信息

您需要在此“YourKey”中设置要访问的密钥。您将获得
[String:Any]
类型的对象。之后,您可以从
dict
对象访问内部值

如果获取
消息
数据,则需要设置
消息
来代替

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
}

愿这对你有帮助。在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"

    }

你能给我们提供你的JSON数据格式吗?“我的JSON数据是其他格式的”什么其他格式?@StacySmith更新了我的格式。@Larme更新了我的格式。请看一看,JSON中有JSON。您需要在其上调用
JSONSerialization
。如果存在等效项,请不要在Swift中使用
NSDictionary
(或NSStuff)。而且,这是一个很大的力展开。有些没有意义:
let alert=aps![“警报”]作为?NSDictionary
后跟
打印(警报!)
@Maheshahane push在我执行此操作后停止接收。我不想打印,我想显示通知。不会调用此方法,而是调用public func userNotificationCenter(center:UNUserNotificationCenter,willPresent通知:UNNotification,withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Swift.Void){}正在调用它。