Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting user's tap on notification

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?

like image 861
Shashank Avatar asked Aug 12 '13 11:08

Shashank


1 Answers

- (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.

like image 147
Baby Groot Avatar answered Sep 22 '22 22:09

Baby Groot