Xcode, Parse - remote notification processing

I am trying to follow the Parsing Guide to handle notifications sent in Json format, but I have 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 application is not crashing now, but the changes I made are not being implemented. Json Code:

 { "aps": { "badge": 10, "alert": "Test", "sound": "cat.caf" }, "url": "http://www.google.com" } 
+6
source share
3 answers

I assume your problem is here:

 launchOptions(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary 

I'm not sure what you expect, but as far as I know, there is no such method in the dictionary. You may be looking for substring syntax. Something like that:

 launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary 

To get the dictionary embedded in the turnkey launchOptions dictionary: UIApplicationLaunchOptionsRemoteNotificationKey.

As they say, launchOptions can be null, you should add this check to your code, and also try to register launchOptions and publish the results here.

You can check if startOptions are null:

 if let launchOpts = launchOptions { var notificationPayload: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary } 
+8
source

NSDictionary objectForKey returns an optional parameter:

func objectForKey(_ aKey: AnyObject) -> AnyObject?

therefore, if you force it to deploy using! you risk if the option contains zero. You should force the deployment only when you are 100% sure that the "Advanced" value contains the value, but in this case the UIApplicationLaunchOptionsRemoteNotificationKey parameter is set only when the application starts, when the user deletes the remote notification message in the notification center.

You should check Optional and dipping on NSDictionary using how ?:

 if let myDict = launchOptions[UIApplicationLaunchOptionsRemoteNotifcationKey] as? NSDictionary { // there is a notification } else { // no notification } 

("NSDictionary" is optional, as you can lower it later, and if you are not discouraged, Swift will warn you that myDict will be displayed as AnyObject.

+3
source
 if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary { } 
+2
source

Source: https://habr.com/ru/post/976379/


All Articles