从推送通知 iOS Swift 4 中获取值

Posted

技术标签:

【中文标题】从推送通知 iOS Swift 4 中获取值【英文标题】:Get Values from Push Notification 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"

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

【问题讨论】:

你想要什么上面的回应? JSON parsing using Swift 4的可能重复 @JigarDarji 我想访问 ID、消息、类型等,但我无法访问它。 【参考方案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

【讨论】:

以上是关于从推送通知 iOS Swift 4 中获取值的主要内容,如果未能解决你的问题,请参考以下文章

SWIFT - 从 userInfo 获取值以填充推送通知的字段

使用firebase问题推送通知(ios,swift 4)[关闭]

推送通知点击获取 url 并在 View Controller , iOS , Swift4.2 中显示

在 Swift 中推送 iOS8 的通知状态

iOS Swift 从推送通知以编程方式导航到某些 ViewController

如何在应用程序处于非活动状态时获取推送通知ios swift3