Ios Swift读取远程通知的用户信息
我实现了一个函数,在收到如下远程通知时打开AlertView:Ios Swift读取远程通知的用户信息,ios,json,swift,push-notification,userinfo,Ios,Json,Swift,Push Notification,Userinfo,我实现了一个函数,在收到如下远程通知时打开AlertView: func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){ var notifiAlert = UIAlertView() var NotificationMessage : AnyObject? = userInfo["alert"]
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()
}
{"aps":{"alert":"Testmessage","badge":"1"}}
但NotificationMessage始终为零
我的json负载如下所示:
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()
}
{"aps":{"alert":"Testmessage","badge":"1"}}
我使用的是Xcode 6,Swift,我正在为iOS8开发。
我搜索了几个小时,但没有找到任何有用的信息。
通知工作正常。。如果我点击它,alertview就会打开。
我的问题是,我无法从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
}
}
请参见,当我从发送消息时,以下代码起作用-
private func extractMessage(fromPushNotificationUserInfo userInfo:[NSObject: AnyObject]) -> String? {
var message: String?
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let alertMessage = alert["body"] as? String {
message = alertMessage
}
}
}
return message
}
与Craing Stanford的答案唯一不同的是我用来从alert
实例中提取消息的键,它是body
,这是不同的。请参阅下文以了解更多信息-
if let alertMessage = alert["message"] as? NSString
vs
应用处于活动状态时,应显示警报。因此,请检查状态是否处于活动状态
如果用户需要此消息,则可以从中获取警报消息
方法(Swift 4):
用法:
let info = self.extractUserInfo(userInfo: userInfo)
print(info.title)
print(info.body)
我使用APNs提供程序和json负载,如下所示
{
"aps" : {
"alert" : {
"title" : "I am title",
"body" : "message body."
},
"sound" : "default",
"badge" : 1
}
}
由于提供程序将其作为JSON定义的字典生成,iOS将其转换为NSDictionary
对象,没有类似dictionary
的下标,但可以使用value(forKey:)
参考自
这是我去斯威夫特4号的路
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guard application.applicationState == .active else { return }
guard let alertDict = ((userInfo["aps"] as? NSDictionary)?.value(forKey: "alert")) as? NSDictionary,
let title = alertDict["title"] as? String,
let body = alertDict["body"] as? String
else { return }
let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
let okAct = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAct)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
completionHandler(UIBackgroundFetchResult.noData)
}
这是我的objC版本
if (userInfo[@"aps"]){
NSDictionary *aps = userInfo[@"aps"];
if (aps[@"alert"]){
NSObject *alert = aps[@"alert"];
if ([alert isKindOfClass:[NSDictionary class]]){
NSDictionary *alertDict = aps[@"alert"];
if (alertDict[@"message"]){
NSString *message = alertDict[@"message"];
}
}
else if (aps[@"alert"]){
NSString *alert = aps[@"alert"];
}
}
}
斯威夫特5
struct Push:可解码{
让AP:aps
结构APS:可解码{
让我们警惕:警惕
结构警报:可解码{
标题:字符串
正文:字符串
}
}
init(解码userInfo:[AnyHashable:Any])抛出{
let data=try JSONSerialization.data(使用jsonObject:userInfo,选项:。预打印)
self=try JSONDecoder().decode(Push.self,from:data)
}
}
用法:
let info = self.extractUserInfo(userInfo: userInfo)
print(info.title)
print(info.body)
guard let push=try?Push(解码:userInfo)else{return}
let alert=UIAlertController(标题:push.aps.alert.title,消息:push.aps.alert.body,首选样式:.alert)
< C/C++ >目标C++中的这个看起来如何?它是好的,你已经告诉了OP根级项是APS,而在USER信息DIC中没有警报。但是如何在iOS系统发送的通知userInfo字典中找到正确的键和字典结构呢。在swift 4文档中,我可以看到通知中所有标准化的通知名称。名称,但我看不到任何文档说。。。这是随特定通知一起发送的userInfo字典,以下是密钥..?是否可以在aps中添加更多参数?