[英]Get Values from Push Notification iOS Swift 4
我在应用程序中实现了推送通知。 当有人向我发送一条消息时,我收到了针对该消息的数据,我想对接收到的数据执行一些操作,但是我无法获取值。 这是我的代码。
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]
这是我的Pretty Printed 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"
}
请指导我如何从中获取关键价值并用于执行某些操作。
你有尝试过吗?
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")]
试试这个代码
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
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.