2

I am having some problems with the behavior push notifications in my app.

If the app is not running (not even in the background), when I touch a notification I can parse its payload and do a custom action, by reading the "launchOptions" dictionary.

When my app is running in the background, I am not being able to detect when a user foregrounds the app by tapping the notification, as application:didReceiveRemoteNotification:fetchCompletionHandler: is called immediately in the background, and when I foreground the app I have no way to check if it was done by tapping the app icon or the notification.

I know this is possible because Facebook's Messenger app does this - if I tap the notification it brings me to the chat, else it brings me to where I was previously.

Thanks.

EduAlm
  • 813
  • 3
  • 11
  • 27
  • Kinda clunky, but why not just use `NSUserDefaults` and set a key? – Oxcug Oct 26 '14 at 21:19
  • I only want an action to happen if the user opens the app using the notification - not tap the icon. FB Messenger does this... So it's possible. – EduAlm Oct 26 '14 at 21:30
  • Yeah, and I'm saying in the `-application:didReceiveRemoteNotification:fetchCompletionHandler:` just set a boolean and check for it when the app comes back to the foreground. – Oxcug Oct 26 '14 at 21:31
  • Still won't work for what I need. I don't want the push action to occur if the user taps the app icon and not the notification itself. – EduAlm Oct 26 '14 at 21:35

1 Answers1

1

If the user taps the app icon, application:didReceiveRemoteNotification:fetchCompletionHandler does not get called.

If they tap the notification, application:didReceiveRemoteNotification:fetchCompletionHandler does get called and application.applicationState will be UIApplicationStateInactive.

Check out this answer for a code snippet that you can use to quickly see this in action to have it make more sense.

Also, to achieve your goal be sure to do any updating of views when the application state is UIApplicationStateInactive, not UIApplicationStateBackground. The application state will be UIApplicationStateBackground when the push notification comes in. The application state will be UIApplicationStateInactive only when the user launches the app by tapping the notification.

I've seen a lot of answers with if statements like

if (application.applicationState != UIApplicationStateActive) { ... }

but that won't allow you to distinguish between a push notification being delivered and the user tapping a notification.

Community
  • 1
  • 1
Jeff Bowen
  • 5,904
  • 1
  • 28
  • 41