当我收到这样的远程通知时,我实现了一个打开 AlertView 的功能:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
var notifiAlert = UIAlertView()
var NotificationMessage : AnyObject? = userInfo["alert"]
notifiAlert.title = "TITLE"
notifiAlert.message = NotificationMessage as? String
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
}
但 NotificationMessage 始终为零。
我的 json 有效负载如下所示:
{"aps":{"alert":"Testmessage","badge":"1"}}
我正在使用 Xcode 6、Swift 并且正在为 iOS8 进行开发。 我现在搜索了几个小时,但没有找到任何有用的信息。 通知工作得很好..如果我点击它,警报 View 就会打开。 我的问题是,我无法从 userInfo 中获取数据。
最佳答案
userInfo
字典的根级项是"aps"
,而不是"alert"
。
尝试以下方法:
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//Do stuff
}
} else if let alert = aps["alert"] as? NSString {
//Do stuff
}
}
见 Push Notification Documentation
https://stackoverflow.com/questions/28596295/