Tuesday, May 7, 2013

Push Notifications in iPhone application - A complete walk through...

APNS(Apple Push Notification Services), It is one of the most innovative service provided by Apple, by which App-Developers are enabled to get in touch with their users personally.

The process seems very simple but the overall backend behind is somewhat a real Apple kind work.

You can add this your application using following steps:

STEP 1: In your application go to the AppDelegate.m file and in the method applicationDidFinishLaunching, add following code,

- (void)applicationDidFinishLaunching:(UIApplication*)application
{
 // Add registration for remote notifications
 [[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |              UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// Clear application badge when app launches
 application.applicationIconBadgeNumber = 0;

///////////////////////////////////////
// Your Previous code resides here. //
///////////////////////////////////////
}
//Fetch and Format Device Token and Register Important Information to Remote Server


STEP 2: In your application go to the AppDelegate.m file  add following 2 Methods,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
 // code to register the device token to the provider
// Use this device token and send it to the Server which will use it to send messages to the device……………….
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 NSLog(@"Error in registration. Error: %@", error);
}

STEP 3: In your application go to the AppDelegate.m file add following Method,
This method receives the notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
 NSLog(@"remote notification: %@",[userInfo description]);
 NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
 NSString *alert = [apsInfo objectForKey:@"alert"];
 NSLog(@"Received Push Alert: %@", alert);
}



No comments:

Post a Comment