Check start with UILocalNotification in Swift

This is the next question. How to check the launch of the Options in Swift? - I got the application to start without crashing, but I cannot seem to detect correctly when the application starts from the notification, and the usual start.

I create my UILocalNotification as follows:

// set up a frequently recurring notification here just for testing... var fast = UILocalNotification() fast.fireDate = NSDate(timeIntervalSinceNow: 15) fast.alertBody = "Alert Message" fast.timeZone = NSTimeZone.localTimeZone() fast.repeatInterval = NSCalendarUnit.CalendarUnitMinute fast.userInfo = ["Important":"Data"] UIApplication.sharedApplication().scheduleLocalNotification(fast) 

And this is my code for trying to process when the application is launched from UILocalNotification.

 func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { if var launch = launchOptions { if var key = launch.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey) { // I never seem to reach this point... } } return true } 

If my application is associated with the background and I click on the warning field, the action I want to run is performed correctly, so I know that at least I can get one path. The problem here is the full launch of the application from the notification.

+6
source share
2 answers

If your application is running in the background, the "application didFinishLaunching" method will not be called. It is already running.

In this case, you must do your job in the "application didReceiveLocalNotification" method.

 func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) { // do your jobs here } 
+3
source

LaunchOptions will be absent if you launch the application directly. The code will be launched if you launch the application through notifications, rather than launching the application directly.

If you run the application directly, you need other ways to solve it. Similar to using LocalNotification time and current time, and decide which view will be displayed.

-1
source

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


All Articles