【问题标题】:Get Values from Push Notification iOS Swift 4从推送通知 iOS Swift 4 中获取值
【发布时间】:2018-06-06 09:34:46
【问题描述】:

我在我的应用程序中实现推送通知。当有人向我发送消息时,我收到了针对该消息的数据,我想对收到的数据执行一些操作,但我无法获取值。这是我的代码。

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")

    guard let data = try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options:.prettyPrinted),
    let prettyPrinted = String(data: data, encoding: .utf8) else { return }
    print("Received direct channel message:\n\(prettyPrinted)")
}

这是我的控制台输出。

Received data message: [AnyHashable("chat"): {"date":"1 second(s) 
ago","img":"http:\/\/adforest-testapp.scriptsbundle.com\/wp- 
content\/plugins\/adforest-rest-api\/images\/user.jpg","ad_id":"439","id":244,"text":"hi","type":"reply"}, 
AnyHashable("adId"): 439, AnyHashable("from"): 170168176816, 
AnyHashable("title"): Honda Civic 2017 Type R, AnyHashable("message"): 
hi, AnyHashable("senderId"): 47, AnyHashable("recieverId"): 1, 
AnyHashable("topic"): chat, AnyHashable("type"): receive]

这是我的漂亮打印 JSON。 收到直接频道消息:

{
"chat" : "{\"date\":\"1 second(s) 
ago\",\"img\":\"http:\\\/\\\/adforest-testapp.scriptsbundle.com\\\/wp- 
content\\\/plugins\\\/adforest-rest-api\\\/images\\\/user.jpg\",\"ad_id\":\"439\",\"id\":244,\"text\":\"hi\",\"type\":\"reply\"}",
"adId" : "439",
"from" : "170168176816",
"title" : "Honda Civic 2017 Type R",
"message" : "hi",
"senderId" : "47",
"recieverId" : "1",
"topic" : "chat",
"type" : "receive"
}

请指导我如何从中获取关键值并用于执行某些操作。

【问题讨论】:

  • 你想要什么上面的回应?
  • @JigarDarji 我想访问 ID、消息、类型等,但我无法访问它。

标签: ios firebase push-notification swift4


【解决方案1】:

你试过了吗?

let chat = remoteMessage.appData[AnyHashable("chat")]
let adId = remoteMessage.appData[AnyHashable("adId")]
let from = remoteMessage.appData[AnyHashable("from")]
let title = remoteMessage.appData[AnyHashable("title")]
let message = remoteMessage.appData[AnyHashable("message")]
let topic = remoteMessage.appData[AnyHashable("topic")]

【讨论】:

    【解决方案2】:
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    
        guard let data = try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options:.prettyPrinted),
        let prettyPrinted = String(data: data, encoding: .utf8) else { return }
        print("Received direct channel message:\n\(prettyPrinted)")
        let chatDic = prettyPrinted["chat"] as Dictionary<String, Any>
        let dataString = chatDic["date"] as String
        let img = chatDic["img"] as String
        let ad_id = chatDic["ad_id"] as String
        let id = chatDic["id"] as Int
        let text = chatDic["text"] as String
        let type = chatDic["type"] as String
    }
    

    【讨论】:

    • 这个答案会给出多个错误,例如有两个“数据”变量。
    • 可能是,因为我现在没有xcode。但它在逻辑上是正确的。 @SharadChauhan
    • prettyPrinted 是 String 类型,不能从 String 中获取类似的值。
    【解决方案3】:

    试试这个代码

    var json = Populate(NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary)
        var chat = json["chat"] as! Chat
             var adId = json["adId"] as! String
             var from = json["from"] as! String
             var title = json["title"] as! String
             var message = json["message"] as! String
             var senderId = json["senderId"] as! String
             var recieverId = json["recieverId"] as! String
             var topic = json["topic"] as! String
             var type = json["type"] as! String
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多