User Discovery on Local Notification

I periodically display a local notification.

UILocalNotification *notification = [[UILocalNotification alloc]init]; [notification setAlertBody:@"Test test"]; [notification setUserInfo:@{@"test": @"test"}]; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 

I need to detect this notification, and I plan to write here.

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

It always calls this function if the user clicks on the notification or is automatically called in the foreground.

So, I share this.

 if (application.applicationState == UIApplicationStateActive) 

When I show the notification center, it becomes InActive. But it still calls didReceiveLocalNotification. I cannot tell if a user can click a notification from the notification center or because of my periodic sending notification.

How can I really know that I am using a notification (either from an InActive state or a Background state) in didReceiveLocalNotification?

+7
ios uilocalnotification
source share
4 answers

Assuming that I understood your problem correctly, I came across the same obstacle and could not find a super-clean solution.

So, the situation when the next method

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

and applicationState equal to UIApplicationStateInactive occurs in two cases:

  • the application is in the foreground and the notification has just been launched.
  • the notification was fired some time ago, the notification center is removed and the user clicked on the notification

One way to distinguish between these two cases is to check the firedate notification:

 notification.fireDate.timeIntervalSinceNow < 0.5 

If this expression is true, it is very likely that the first case occurred. If the expression is false, it is very likely that a second case has occurred.

This decision depends on the system providing the notification without delay, and / or the user is not fast enough to click the notification in the notification center up to 500 ms after the notification starts. I'm not sure how likely this will be. I think this is possible if the device is under some kind of processing load.

I hope there is a cleaner solution, hope someone shares it.

+7
source share

First , read this from the Apple Documentation :

The user enters the custom action button in the iOS 8 notification. In this case, iOS calls either application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler: In both methods, you get the action identifier so that you can determine which button the user tapped. You also receive either a remote or local notification object so that you can receive any information you need to process the action.

The user removes the default button in the alert or taps (or clicks) the application icon. If the default action button is pressed (on an iOS device), the system launches the application, and the application calls its delegates application:didFinishLaunchingWithOptions: passing in (for remote notifications) or a local notification object (for local notifications). Although application:didFinishLaunchingWithOptions: Not the best place to handle the notification, getting the payload at this point gives you the opportunity to start the process of updating to your handler method called.

Second , so you can tell if didReceiveLocalNotification: from an active or inactive state:

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState appState = UIApplicationStateActive; if ([application respondsToSelector:@selector(applicationState)]) appState = application.applicationState; if (appState == UIApplicationStateActive) { } else { } } 
+5
source share
  • Application: didReceiveLocalNotification:

Sent to a delegate when a running application receives a local notification.

Check this:

iOS UILocalNotification - when the application starts in the background, delegation methods are not running, and the icon is displayed by notification

+1
source share

Use KVO-value-observation to know and do something at the click of a button.

-3
source share

All Articles