我正在尝试按照解析指南来处理我以 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
}
应用程序现在没有崩溃,但我所做的更改没有生效。 json代码:
{
"aps": {
"badge": 10,
"alert": "Test",
"sound": "cat.caf"
},
"url": "http://www.google.com"
}
最佳答案
我猜你的问题出在这里:
launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
我不确定您在期待什么,但据我所知,字典上没有这样的方法。您可能正在寻找下标语法。像这样:
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary
获取嵌套在键下的 launchOptions 字典中的字典:UIApplicationLaunchOptionsRemoteNotificationKey。
也就是说,launchOptions 可能为 nil,您应该在代码中添加该检查,并且尝试记录 launchOptions
并在此处发布结果。
您可以像这样检查 launchOptions 是否为 nil:
if let launchOpts = launchOptions {
var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}
关于Xcode、Parse - 处理远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244474/