Nick
Nick

Reputation: 906

iOS 5 retrieve information from a sent Push Notification using the NSDictionary

Is it possible to retrieve the information from a sent Push Notification using the NSDictionary? (For example, getting the title, message and sound that the alert payload contained).

I also want to send information in the payload (such as a string) for the app to use that isn't related to the title or message. Again, is this possible?

Upvotes: 3

Views: 4033

Answers (2)

tilo
tilo

Reputation: 14169

Yes, both is possible!

Referring to getting the desired information, do the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Push notification was received when the app was in the background

    // ..... 
    if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            NSLog(@"Launched from push notification: %@", dictionary);
            // do something with your dictionary
        }
    }
    // ..... 
    return YES;
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    //  Push notification received while the app is running

    NSLog(@"Received notification: %@", userInfo);
    // do something with your dictionary
}

Upvotes: 8

dtuckernet
dtuckernet

Reputation: 7895

Yes you can get this information. Inside of the userInfo NSDictionary instance there is a property (which contains another NSDictionary) under the key aps. This contains additional properties for alert, badge, and sound keys.

Your custom information that gets passed along will be present in the userInfo NSDictionary instance under the parameters that you gave it when the push notification was sent.

See the UIApplicationDelegate protocol reference for more information: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Upvotes: 2

Related Questions