Ever wondered how to make that banner notification show up at top of an iPhone?

Here’s how you do it.

1. Define a local notification.

ViewController.m

- (void)viewWillAppear:(BOOL)animated {
    [self setupLocalNotifications];    
}

- (void)setupLocalNotifications {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    // current time plus 10 secs
    NSDate *now = [NSDate date];
    NSDate *dateToFire = [now dateByAddingTimeInterval:5];
        
    NSLog(@"now time: %@", now);
    NSLog(@"fire time: %@", dateToFire);

    localNotification.fireDate = dateToFire;
    localNotification.alertBody = @"Time to get up!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1; // increment
    
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
    localNotification.userInfo = infoDict;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

This basically setup a local notification to fire 5 seconds in the future from when the app view comes on screen.

2. Register.

We register for the notification and look for it in AppDelegate.

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    
    if (notification) {
        [self showAlarm:notification.alertBody];
        NSLog(@"AppDelegate didFinishLaunchingWithOptions");
        application.applicationIconBadgeNumber = 0;
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [self showAlarm:notification.alertBody];
    application.applicationIconBadgeNumber = 0;
    NSLog(@"AppDelegate didReceiveLocalNotification %@", notification.userInfo);
}

3. Display the alert.

AppDelegate.m

- (void)showAlarm:(NSString *)text {
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm" 
                                                        message:text delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
	[alertView show];
}

This gets called from the AppDelegate whenever the notification is recieved. Here we are popping up an alert, but this could manifest itself as an alert or banner (more on that in a second).

How to test

Fire up the app, put your app in the background (via the home button), wait 5 seconds, and voila, you should see this

or this

User chooses how to display

Now if you go through this example and don’t see a banner showing up at the top of your screen (you get an alert instead) it’s one of two things.

1. Banners don’t show up on simulators.

To see the banner you need to connect a real iPhone up to Xcode and run the example there.

2. Users choose how to display alerts.

In iOS5 there are two notification styles: alert or banner. Users choose which they would like at see in application settings (this is on a per app basis).

And as of now there is not way to choose or set this default up for them progammatically (though I believe the default is banner).

Good luck and happy notifying!

Links that help

http://stackoverflow.com/questions/8229385/set-default-ios-local-notification-style-for-application