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:
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:
{
"aps": {
"badge": 10,
"alert": "Test",
"sound": "cat.caf"
},
"url": "http://www.google.com"
}
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.
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.
You can check if launchOptions are nil like this:
if let launchOpts = launchOptions {
var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}
NSDictionary's objectForKey
returns an optional:
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.
You must check for the Optional and downcast to NSDictionary using as?:
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.
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With