Detect if application received focus due to UILocalNotification

I am creating an application that tracks some reminders that are repeated at a user-specified interval.

I did this when a warning appears, the name of the action says โ€œRefreshโ€. When you click this, the application will open, and here I want to create the following reminder, but the problem is that I do not know if the application opens because the notification button was pressed or if the notification was launched while the application was running.

Does anyone have any idea?

+7
source share
6 answers

The โ€œrightโ€ way to do this is to check the NSApplication applicationState property in the application: didReceiveRemoteNotification: your delegate method.

From the documentation for handling local notifications:

iOS Note. In iOS, you can determine whether the application is launched as a result of a user clicking an action button or a notification has been sent to an already running application studying the state of the application. In delegates, the implementation of the application: didReceiveRemoteNotification: or application: didReceiveLocalNotification: method, get the value of applicationState and evaluate it. If the value is UIApplicationStateInactive, the user clicked the action button; if Value is UIApplicationStateActive, the application was it received a notification.

This is similar to your solution using flags set in applicationWillEnterForeground and applicationDidBecomeActive, but with system support.

+13
source

I donโ€™t know if my question was incomprehensible, but it seems that I had 4 different answers that all misinterpreted my question: P

However, I found that didReceiveLocalNotivication occurs between willEnterForeground and didBecomeActive, so I did this to determine if the application was open or not:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"Opened from notification? %@", wasInactive ? @"yes!" : @"no!"); } - (void)applicationWillEnterForeground:(UIApplication *)application { wasInactive = YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { wasInactive = NO; } 
+13
source

You are looking for

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

or

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

Documentation

0
source

See the documentation for the UIApplication startup option keys. Last parameter for your application: didFinishLaunchingWithOptions: the delegation method contains the necessary information.

Also see the application: didReceiveLocalNotification .

0
source

If your application is already running, you will receive this delegate message in the application delegate

 application:didReceiveLocalNotification: 

If it was not running, you will have to use

 application:didFinishLaunchingWithOptions: 

You need to answer accordingly in both methods to cover all cases.

UPDATED

To determine if a user has activated an action button, it takes a little harder. We can say that application:didFinishLaunchingWithOptions: will have a local notification as a launch option, but it is more complicated with application:didReceiveLocalNotification:

Since the application becomes active after the user clicks the button, we must delay until we see this message (or not). Set NSTimer to application:didReceiveLocalNotification and application:didReceiveLocalNotification it in didBecomeActive . This means that the user clicked the action button. If the timer is not canceled, the user was inside the application when it was started.

0
source

In your delegate deletion in this method:

 - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

You need to learn startOptions by looking at this key:

UIApplicationLaunchOptionsLocalNotificationKey

When you are already active, it will be called:

 - (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
0
source

All Articles