How to get custom push notification value from launchOptions with fast?

I am building an application with Swift that receives push notifications. I am sending custom values ​​inside JSON.

I open the application through a notification, so I know that I need to do this inside "didFinishLaunchingWithOptions" and read the value from "launchOptions".

How can I read these values ​​and use them in my application.

Thank you very much.

+6
source share
2 answers

Here is what works for me in SWIFT 2 when your application does not start. The code is not very elegant due to additional bindings. But it does work.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // if launched from a tap on a notification if let launchOptions = launchOptions { if let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] { if let action = userInfo["action"], id = userInfo["id"] { let rootViewController = self.window!.rootViewController as! ViewController let _ = setTimeout(5.0, block: { () -> Void in rootViewController.openNotification(action as! String, id: id as! String) }) } } } return true } 
+4
source

In the application: didReceiveRemoteNotification: fetchCompletionHandler, user data is passed to didReceiveRemoteNotification, which is an NSDictionary. The details you want to receive are probably on the "aps" key of userInfo.

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary!) { var notificationDetails: NSDictionary = userInfo.objectForKey("aps") as NSDictionary } 

When the application is not running, you will need to get it from the application: didFinishedLaunchWithOptions,

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { if let launchOpts = launchOptions { var notificationDetails: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary } return true } 

EDIT: Remote Notification Syntax Syntax

+1
source

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


All Articles