Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode, Parse - Handling remote notifications

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"
}
like image 382
Abdou023 Avatar asked Oct 07 '14 20:10

Abdou023


3 Answers

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
}
like image 151
Logan Avatar answered Nov 14 '22 17:11

Logan


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.

like image 38
viggio24 Avatar answered Nov 14 '22 17:11

viggio24


if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

}
like image 2
MobileMon Avatar answered Nov 14 '22 17:11

MobileMon