5
votes

According to apple docs, we can identify user's tap on notification by checking whether the application state variable is inactive (link).

iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.

But I can see a use case where there is a system alert (By system alert, I mean an alert view presented in the foreground of the app which is controlled by iOS) in foreground and the app is in inactive state (When a "system alert" is in view, the app behind is made inactive by iOS by setting app's application state to UIApplicationStateInactive) but the user will still be able to see the app's content on the screen. Refer the attachment below:

enter image description here

At this state if the app receives a notification, it will behave as though user tapped on notification. Is there a solution to solve this use case?

2
Quite confusing. What do u exactly mean by "system alert in foreground and the app is in inactive state"?Baby Groot
@HinataHyuga In the screenshot I have an example of a system alert (notification alert on gmail app) which is controlled by iOS and not the app behind. At this point iOS treats the app behind as it is in inactive state (app's applicationstatus will be set to inactive).Shashank
each time application:didReceiveLocalNotification: method call when you got notification.iPatel
Yes @iPatel. But what ideally should be the condition for checking whether user tapped on the notification.Shashank
You want for handle notification when app is active or inactive?Baby Groot

2 Answers

5
votes
- (void)application:(UIApplication*)application didReceiveRemoteNotification: 
(NSDictionary*)userInfo
{
         UIApplicationState state = [application applicationState];
         if (state == UIApplicationStateActive)
         { 
              //When your app was active and it got push notification
         }
         else if (state == UIApplicationStateInactive) 
         {
              //When your app was in background and it got push notification
         }
}

and didFinishLaunchingWithOptions will be called when your app was not running and Launch was clicked in your notification.

As you will display alertview in didReceiveRemoteNotification, you can identify tap in alertview's delegate method i.e. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex.

0
votes

They fix this issue on ios10, when the add method :

userNotificationCenter willPresentNotification.

this will only called when the app is in forground/active state, and notification will appear only after the user dismiss the other system alert.