Posting a Message in iOS Notification Center

iOS Notification Center allows an application to notify the user without the interruption of using a popup dialog. Previously, the UILocalNotification API allowed a foreground or background application to show messages using a popup dialog. However, this proved to be an annoyance when a popup was posted by a backgrounded application and the user was in the middle of working with the application in foreground. The UILocalNotification API has now been migrated to use the notification center. In this tutorial, we will learn the basics of using the notification center.


When a user pulls down the notification center and then taps on a notification, iOS calls the  application:didReceiveLocalNotification: method of the application delegate. Hence, it is perhaps better for the delegate to also post the messages. This way, all notification related code is centralized in one class.

In our application delegate class, we will define a class level method that posts a new notification message:

+ (void) postNotification: (NSString *) message {
    NSLog(@"Posting notification");

    UILocalNotification *n = [[UILocalNotification alloc] init];

    n.alertBody = message;
    //Post the notification after 10s
    n.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    n.timeZone = [NSTimeZone defaultTimeZone];

    NSMutableDictionary * d = [[NSMutableDictionary alloc] init];
    [d setObject:@"Some data" forKey:@"data"];
    n.userInfo = d;

    [[UIApplication sharedApplication] scheduleLocalNotification:n];

Any view controller can post a message as follows.

[MyAppDelegate postNotification:@"Hello World"];

Also in the application delegate class, we add this method to deal with notification event handling:

- (void) application:(UIApplication *) application 
    didReceiveLocalNotification:(UILocalNotification *)n {

    NSLog(@"Received notification");

    NSDictionary *d = n.userInfo;

    if (d != nil) {
        NSString* str = [d objectForKey:@"data"];
        NSLog(@"Received data: %@", str);
    } else {
        NSLog(@"No data was received");

How this event handler gets called depends upon whether the application is in the foreground when the message is posted.

Foreground – When the message is posted (10s after the postNotification: method call in our example), if the application is in foreground, the application:didReceiveLocalNotification: method is called right away. The notification is also posted in the Notification Center.

Background – When the message is posted (10s after the postNotification: method call in our example), if the application is in background, then only the message is posted in the Notification Center.

At any point, user can pull down the Notification Center and tap on the message. iOS calls the application:didReceiveLocalNotification: of the application delegate. If the application is in background, iOS will first bring it to foreground before calling application:didReceiveLocalNotification:.

As a final thought, you may want to remove the notification once the event for it has been handled. Otherwise, user will have to manually remove it.

- (void) application:(UIApplication *) application 
    didReceiveLocalNotification:(UILocalNotification *) n {

    [[UIApplication sharedApplication] cancelLocalNotification:n];

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.