我正在使用以下库来生成推送通知。
https://github.com/edamov/pushok
我使推送通知正常工作,但是我不知道如何在Swift 3中提取响应。
这就是我所拥有的。
// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
// Print notification payload data
print("Push notification received: \(data)")
let aps = data[AnyHashable("aps")]!
print(aps)
}
我可以创建推送通知,但控制台消息有效,但可以打印出来...
Push notification received: [AnyHashable("samplekey"): samplevalue, AnyHashable("aps"): {
alert = {
body = hey;
title = "Hello!";
};
sound = default;
}]
{
alert = {
body = hey;
title = "Hello!";
};
sound = default;
}
因此,我的问题是如何访问警报中“身体”和“标题”的数据?
我尝试访问变量,但始终出错,因为我不确定应该如何访问它,并且在任何教程中都找不到有关此主题的任何文档。
我认为这是约瑟夫发现的更安全的方法。
guard
let aps = data[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
}
print("Title: \(title) \nBody:\(body)")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句