在iOS中通过通知获取有效载荷数据的问题

时间:2019-06-10 13:06:41

标签: ios swift push-notification

我从服务器端收到通知,并且试图从中获取数据,我的有效载荷是这样的:

[AnyHashable("title"): New message received, AnyHashable("gcm.notification.data"): {"FilePath":null,"IsAttachment":false,"UserName":"Muhammad Hassan","User":null,"StrUnPublishDate":null,"Message":"Eggshells","UserImage":"http:\/\/gigjobsadmin.arhamsoft.com\/Content\/User\/36\/16-04-2019 11-460.jpg","User1":null,"ToUserID":null,"ToUserId":132,"SenderId":null,"IsRead":false,"Code":null,"StrPublishDate":null,"StrDateTime":null,"Type":null,"Active":null,"StrCreatedDate":null,"ThreadId":46,"Search":null,"FromUserId":36,"CreatedDate":"2019-06-10T23:00:43.5972809Z","Id":1109,"BatchCount":0}, AnyHashable("gcm.message_id"): 0:1560171729408562%3724072637240726, AnyHashable("data"): {"FilePath":null,"IsAttachment":false,"UserName":"Muhammad Hassan","User":null,"StrUnPublishDate":null,"Message":"Eggshells","UserImage":"http:\/\/gigjobsadmin.arhamsoft.com\/Content\/User\/36\/16-04-2019 11-460.jpg","User1":null,"ToUserID":null,"ToUserId":132,"SenderId":null,"IsRead":false,"Code":null,"StrPublishDate":null,"StrDateTime":null,"Type":null,"Active":null,"StrCreatedDate":null,"ThreadId":46,"Search":null,"FromUserId":36,"CreatedDate":"2019-06-10T23:00:43.5972809Z","Id":1109,"BatchCount":0}, AnyHashable("body"): Eggshells, AnyHashable("badge"): 1, AnyHashable("google.c.a.e"): 1, AnyHashable("aps"): {
alert =     {
    body = Eggshells;
    title = "New message received";
};
badge = 1;
category = ".MainActivity";
"content-available" = 1;
}]

当我从中获得aps时,它可以正常工作,但是当我尝试从“数据”键中查找时,它显示为nil。这就是我获取价值的方式,

let delegate = UIApplication.shared.delegate as! AppDelegate
    let userInfo = delegate.userInfo
    print(userInfo!)


    let data = userInfo!["data"] as? NSDictionary

    let message = data!["Message"] as? String
    let userImage = data!["UserImage"] as? String
    let fromUserId = data!["FromUserId"] as? Int
    let createdDate = data!["CreatedDate"] as? String

    guard
        let aps = userInfo![AnyHashable("aps")] as? NSDictionary,
        let alert = aps["alert"] as? NSDictionary,
        let body = alert["body"] as? String,
        let title = alert["title"] as? String
        else {
            // handle any error here
            return
    }

    guard let badge = userInfo![AnyHashable("badge")] as? Int
        else
    {
        return
    }

2 个答案:

答案 0 :(得分:1)

data的值是JSON字符串,而不是字典。您必须分别反序列化

struct NotificationData : Decodable {

    let message : String
    let userImage : URL
    let fromUserId : Int
    let createdDate : String


    private enum CodingKeys: String, CodingKey { case message = "Message", userImage = "UserImage", fromUserId = "FromUserId", createdDate = "CreatedDate" }
}


let delegate = UIApplication.shared.delegate as! AppDelegate
guard let userInfo = delegate.userInfo as? [String:Any],
      let notificationString = userInfo["data"] as? String else { return }
let data = Data(notificationString.utf8)
do {
    let result = try JSONDecoder().decode(NotificationData.self, from: data)
    let message = result.message
    let userImage = result.userImage
    let fromUserId = result.fromUserId
    let createdDate = result.createdDate
} catch { print(error) }

答案 1 :(得分:0)

替换

let data = userInfo!["data"] as? NSDictionary 

收件人

if let data = userInfo!["data"] as? [AnyHashable, Any]{
print(data)
}