代码之家  ›  专栏  ›  技术社区  ›  Ravi

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

  •  -1
  • Ravi  · 技术社区  · 8 年前

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

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

    { 
      "aps": {
             "alert":"Testing.. (0)",
             "badge":1,
             "sound":"default",
             "mutable-content": 1,
             "attachment-url": ""
             }
    }
    

    但是我的 数据以其他格式出现,我无法解析它以显示正确的通知。

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

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

    2 回复  |  直到 8 年前
        1
  •  1
  •   Pravin    8 年前

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

    您需要在此“YourKey”中设置要访问的密钥。你会得到 [String: Any] dict 对象。

    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
    }
    
        2
  •  3
  •   Mahesh Shahane    8 年前

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