How to manage notification when users click on badge

JackTurky picture JackTurky · Dec 31, 2011 · Viewed 15.1k times · Source

I have a question to ask about notifications. After some hours to learn how to implement push notifications on iPhone, it now arrives!

How do I manage users that click on badge or click view on alert? What happen when users click there?

I tried to send me some notification and the number on the icon of application in springboard increments. In which way clicking there it's possible to show a uiview to manage the notification arrived and show the message read and unread?

Is there a tutorial for it? I want to save all the messages inside a uitableview.

Answer

Shane Powell picture Shane Powell · Dec 31, 2011

You want to read Handling Local and Remote Notifications

Basically in your application delegate, you want to implement:

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

and

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

And process the launchOptions / userInfo for the notification data.

How I normally process the data is:

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary* userInfo =
        [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self processRemoteNotification:userInfo];
    }
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [self processRemoteNotification:userInfo];
}

The format for userInfo is documented the The Notification Payload section.

e.g. the "aps" key will give you another NSDictionary, then looking up the "alert" key will give you the alert message that was displayed. Also, any custom data you send in the JSON payload will be in there as well.

NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alertMsg = @"";
NSString *badge = @"";
NSString *sound = @"";
NSString *custom = @"";

if( [apsInfo objectForKey:@"alert"] != NULL)
{
    alertMsg = [apsInfo objectForKey:@"alert"]; 
}


if( [apsInfo objectForKey:@"badge"] != NULL)
{
    badge = [apsInfo objectForKey:@"badge"]; 
}


if( [apsInfo objectForKey:@"sound"] != NULL)
{
    sound = [apsInfo objectForKey:@"sound"]; 
}

if( [userInfo objectForKey:@"Custom"] != NULL)
{
    custom = [userInfo objectForKey:@"Custom"];
}