Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect user clicked iOS remote notification?

When user taps on a remote notification, the following callback is triggered in the app delegate:

-application:didReceiveRemoteNotification:fetchCompletionHandler:

in this scenario, application is launched and the app state is UIApplicationStateActive which I interpret it as user actioned on a remote notification.

the problem: This method can also get called when a remote notification arrives and app is in the foreground with inactive state.

example: when notification center view is open(swipe from top edge of screen down) or a UIAlert is open. In both case application state is UIApplicationStateActive and there is no way to tell whether it's a user actioned notification or system push received.

Q: How can I determine if didReceiveRemoteNotification callback is response to user tapping on remote notification vs arrival of remote notification?

like image 346
Mehrdad M Avatar asked Nov 09 '22 04:11

Mehrdad M


1 Answers

UIApplicationState state = [application applicationState];
     if (state == UIApplicationStateActive)
     { 
          //When your app is in foreground and it received a push notification
     }
     else if (state == UIApplicationStateInactive) 
     {
          //When your app was in background and it received a push notification
     }

Also, didFinishLaunchingWithOptions will be called if the app was not running and the user tapped a notification. I have not tried it but i can assume you can get the notification details from options.

like image 66
gomezluisj Avatar answered Nov 15 '22 12:11

gomezluisj