how can I handle push notification when my app is not running

Ali picture Ali · Jan 24, 2011 · Viewed 8k times · Source

how can I handle push notification when my app is not running i am developing for ios3 .please help

Answer

Di Wu picture Di Wu · Jan 24, 2011

A short example:

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

    NSDictionary *tmpDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];

    //if tmpDic is not nil, then your app is launched due to an APNs push, therefore check this NSDictionary for further information
    if (tmpDic != nil) {
        NSLog(@" - launch options dict has something ");
        NSLog(@" - badge number is %@ ", [[tmpDic objectForKey:@"aps"] objectForKey:@"badge"]);
        NSLog(@" - ");
    } 

Edited: About the NSDictionary you received, from Apple's official documentation:

You can access the contents of the aps dictionary—though you shouldn’t need to in most cases—using the following keys:

  1. alert—The value may either be a string for the alert message or a dictionary with two keys: body and show-view. The value of the former is the alert message and the latter is a Boolean (false or true). If false, the alert’s View button is not shown. The default is to show the View button which, if the user taps it, launches the application.
  2. badge—A number indicating the quantity of data items to download from the provider. This number is to be displayed on the application icon. The absence of a badge property indicates that any number currently badging the icon should be removed.
  3. sound—The name of a sound file in the application bundle to play as an alert sound. If “default” is specified, the default sound should be played.

The dictionary may also have custom data defined by the provider according to the JSON schema. The properties for custom data should be specified at the same level as the aps dictionary. However, custom-defined properties should not be used for mass data transport because there is a strict size limit per notification (256 bytes) and delivery is not guaranteed.