Thursday, May 12, 2011

Push Notification in iPhone SDK

Hello Friends,

Now lets look at push notification functionality in iPhone SDK. Push notification is very attractive feature of iPhone SDK.

  • For implementing push notification we have to register device with the server. We have to write code for this functionality in AppDelegate method of application. We will write code in didFinishedLaunching method.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];


  • Following code will get called when application will launch. Following method will be called only one time at starting of application.

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken 
{ self.deviceToken = [[[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

//In below methods, deviceToken pass to register device on Web service that send push notification.
[self saveToUserDefaults:self.deviceToken];

//NSLog(@"%@",self.deviceToken);

} 


  • Now we have to write function that will catch the response which will be sent by server for push notification. And we will add some functionality in response. For this, enter following code in App Delegate.

//this method is call when applcation is running and notification is come.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{

// Do whatever when application receive notification that will written in this method.

UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"Notification" message:@"alert" delegate:self cancelButtonTitle:@"Update" otherButtonTitles:nil];
[alertview show];
[alertview release];
}

No comments:

Post a Comment