Search code examples
iosnotificationspush-notificationforeground

ios Proper way to determine if the method "applicationWillEnterForeground" is called due to a PushNotification action


I'm working on an app that does a tasks "A" in - (void)applicationWillEnterForeground:(UIApplication *)application.

The app is also registered for remote notifications. In - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo the app needs to execute a task "B".

When the app is background and a notification is received the order of task execution is "A", followed by "B".

What i need to do, in case a notification is received and the app is in background, is to execute just the "B" task.

How can one accomplish this?

Thanks


Solution

  • It's been a month since you posted this question so I'm not sure if you still need an answer, but I solved this problem by pushing all shared "entrance logic" (as I like to call it) to applicationDidBecomeActive: and simply store the entrance method as state in the appdelegate object. So if I come into the app from the background, then I set one flag. If I come in through a notification, I store the notification message (so I can intelligently process the notification entrance logic). If I came in through a url, then I store that url. In applicationDidBecomeActive: I look to see which way I came in from. Here are the states that I watch for:

    1. New app instance by tapping on app icon (no state set)
    2. Came in from background by tapping on app icon or ending a phone call, etc (only enter foreground flag set, nothing else)
    3. Came in from APNS (notification data is set, note this could be either new app instance or from background)
    4. Came in from url (url is set, note this could be either new app instance or from background)

    With all the necessary pieces of information in one place, I can intelligently decide if I want to execute the notification entrance logic, or the simple enter foreground entrance logic.

    BTW, this flow chart is invaluable! http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/