Xcode, Parse - 处理远程通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26244474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode, Parse - Handling remote notifications
提问by Abdou023
I'm trying to follow the Parse Guide to handle the notifications i send in Json format, but i'm having a problem, here is my code:
我正在尝试按照解析指南来处理我以 Json 格式发送的通知,但我遇到了问题,这是我的代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
Parse.setApplicationId("MyAppId", clientKey: "MyAppClientKey")
var notificationType: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
if let launchOptions = launchOptions {
var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!
println(launchOptions)
var url = notificationPayload["url"] as String
var feed: FeedTableViewController = FeedTableViewController()
feed.messages.insert(url, atIndex: 0)
feed.sections.insert("section", atIndex: 0)
}
return true
}
the app doesn't crash now, but the changes i made doesn't take place. Json code:
该应用程序现在不会崩溃,但我所做的更改不会发生。JSON代码:
{
"aps": {
"badge": 10,
"alert": "Test",
"sound": "cat.caf"
},
"url": "http://www.google.com"
}
回答by Logan
I'm guessing your problem is here:
我猜你的问题在这里:
launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
I'm not sure what you're expecting, but as far as I know, there's no method like that on a dictionary. You might be looking for subscript syntax. Something like this:
我不确定你在期待什么,但据我所知,字典上没有这样的方法。您可能正在寻找下标语法。像这样的东西:
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary
To get a dictionary nested within the launchOptions dictionary under the key: UIApplicationLaunchOptionsRemoteNotificationKey.
获取一个嵌套在 launchOptions 字典中的字典,键下:UIApplicationLaunchOptionsRemoteNotificationKey。
That being said, launchOptions could be nil, you should add that check in your code and also, try logging launchOptions
and post the results here.
话虽如此,launchOptions 可能为零,您应该在代码中添加该检查,并尝试记录launchOptions
并在此处发布结果。
You can check if launchOptions are nil like this:
您可以像这样检查 launchOptions 是否为零:
if let launchOpts = launchOptions {
var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}
回答by viggio24
NSDictionary's objectForKey
returns an optional:
NSDictionaryobjectForKey
返回一个可选的:
func objectForKey(_ aKey: AnyObject) -> AnyObject?
func objectForKey(_ aKey: AnyObject) -> AnyObject?
so if you force unwrap it using the ! you take your risk if the Optional contains nil. You should force unwrap only when you're 100% sure that the Optional contains a value but in this case the UIApplicationLaunchOptionsRemoteNotificationKey is set only when the application is launched when the user taps on the remote notification message in the notification center.
因此,如果您使用 ! 如果 Optional 包含 nil,您将承担风险。只有当您 100% 确定 Optional 包含一个值时,您才应该强制解包,但在这种情况下, UIApplicationLaunchOptionsRemoteNotificationKey 仅在用户点击通知中心的远程通知消息时启动应用程序时才设置。
You must check for the Optional and downcast to NSDictionary using as?:
您必须使用 as? 检查 Optional 和向下转换到 NSDictionary :
if let myDict = launchOptions[UIApplicationLaunchOptionsRemoteNotifcationKey] as? NSDictionary {
// there is a notification
} else {
// no notification
}
(the "as? NSDictionary" is not strictly required, as you can downcast later; if you don't downcast Swift will warn you that the myDict object will be inferred as AnyObject.
(“as?NSDictionary”不是严格要求的,因为你可以稍后向下转换;如果你不向下转换,Swift 会警告你 myDict 对象将被推断为 AnyObject。
回答by MobileMon
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
}