Shashank
Shashank

Reputation: 1753

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?

Upvotes: 5

Views: 5356

Answers (2)

user1105951
user1105951

Reputation: 2287

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.

Upvotes: 0

Baby Groot
Baby Groot

Reputation: 4279

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

Upvotes: 5

Related Questions