views:

1097

answers:

4

I am sending Push Notifications to my iPhone app, and I'd like a different set of instructions to execute depending on whether the app is already launched or not. I'm new to iPhone development, and while I suspect UIApplication or my project's AppDelegate class has the solution, I haven't found a good answer. Is there an easy way to check for this?

+1  A: 

The Apple documentation for push notifications explains this:

However, there are two situations where applicationDidFinishLaunching: is not a suitable implementation site:

  • The application is running when the notification arrives.
  • The notification payload contains custom data that the application can use.

In the first case, where the application is running when iPhone OS receives a remote notification, you should implement the application:didReceiveRemoteNotification: method of UIApplicationDelegate if you want to download the data immediately. After downloading, be sure to remove the badge from the application icon. (If your application frequently checks with its provider for new data, implementing this method might not be necessary.)

This means that if your application:didReceiveRemoteNotification: delegate method is called, your app is running.

Adrian Kosmaczewski
+3  A: 

The UIApplication delegate has the method

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

which you need to implement. This receives the notification when the app is running.

If your app is not currently running and a notification is received then your app can be launched with

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

with the notification details held in the launchOptions dictionary. if the dictionary is nil then the user tapped the application icon as normal.

Kevin
+3  A: 

THAT IS ACTUALLY PARTIALLY TRUE.This is the correct code to use:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // get state
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
   //the app is in the foreground, so here you do your stuff since the OS does not do it for you
   //navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
            }

    application.applicationIconBadgeNumber = 0;
}

didReceiveRemoteNotification is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does NOTHING, and just calls your didReceiveRemoteNotification.

Marco Papa
A: 

Well this is not what i got.

1st case : My app is running and i send a notification, my app display an alertview with some basic text that i put on the application:didReceiveRemoteNotification: method and this method is called so it looks like the user successfully received my notification while he uses my app. So the documentation is correct.

2nd case : I leave my app and send a notification and I touch the "View" button so it lauches my app, the application:didReceiveRemoteNotification: method is also called, so there s 2 alerts :

  • one that is displayed by the notification I sent
  • one that is displayed when the app is launched (cause the application:didReceiveRemoteNotification: method is called)

So the user has 2 alerts and it pretty much ruins the experience.

3rd case : my app is not running, i send a notification, and I touch the "Close" button ; then i launch my app, the application:didReceiveRemoteNotification: method is not called so my alertview is not displayed.

In iOS 4.0 there is the UIApplicationState class that could help but I want to also want to target iOS 3.x users.

Any help?

Aksam