Sunday, August 14, 2011

Push Notification iOS Side Programming

Push Notification (also called Remote Notification) is a powerful free function provided by Apple. App can use it to push promotions or other tailor-made messages to App users.

iOS side code is simple, most of time within 200 lines. However, the logic part is a little bit difficult if you want to write an "intelligent" handler when push message comes, taking consideration of the following three scenarios:
  1. App is not running. 
  2. App is running at the background.
  3. App is running at the forefront.
Here, I am going to share some of the iOS side code for "Register for Push Notification" and "Handling of Push Notification".

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
[[UIApplication sharedApplicationregisterForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

        //////////Your App launch Setups
        //////////Your App launch Setups
        //////////Your App launch Setups
        [self.window makeKeyAndVisible];
//for receiving push notification when the App is not running.
if (launchOptions!=nil) {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
NSDictionary * userInfo=[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//this userInfo actually contains information about the received Push notification, so write some code about it and do handling about it here. 
}
}
    return YES;

}


-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//send the token to your server here
//send the token to your server here
//send the token to your server here
}

Please take a look at the above code.
First line is to Register for Push Notification, and second line is to clear BadgeNumber if there is any.

Let's go to next function first,
-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken.





This function is called when the App receive the "token" from Apple, so you'd write some code to send this code to your server (Better hash it first). With this token, your server will be able to send Push Notification to the user with this "token".

Move back to function (didFinishLaunchingWithOptions), near there end the few lines of code is actually handler for scenario 1: App is not running. 

Handler for scenario 2 & 3 will be handled by the following function, basically it is the place you can capture the message (userInfo) for the Push Notification. In this example, I use an AlertView to display the Push message for scenario 3, which looks intuitive. Then, any further handling with be written within the AlertViewDelegate methods.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Got Notification%@",userInfo);
if ( application.applicationState == UIApplicationStateActive ){
// app was already at the forefront: Scenario 3
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]  delegate:self
  cancelButtonTitle:@"Cancel"
  otherButtonTitles:@"View", nil];
[alertView show];
[alertView release];
}
}
else// app was just brought from background to forefront: Scenario 2
{
//Write your code here to handle your Push Notification
}
}


Hope this helps some people. It is not very in detail, but the structure is there. Thank you.