The push notification is quite complex part to handle. Get it done in only Three easy steps
1] Register for push notification
2] Get Device token
3] Implement delegate methods to handle push notification
1] Register for push notification
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// register for remote notification
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert )];
//handling push notification: when app is not in running state either in background or foreground
if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey])
{
NSLog(@”received notification: %@”,launchOptions); //handle notification here with launchOptions dictionary
}
}
2] Get Device token
Receive token in delegate method
– (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
NSString *tokenStr = [devToken description]; //here is the device token needed for Push notification
}
//delegate method for getting failure error of registering device
– (void)application:(UIApplication *)app
didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(@”Error in registration. Error: %@”, err);
}
3] Implement delegate methods to handle push notification
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//called when application is running either in foreground or background
NSLog(@”received notification: %@”,userInfo); //handle notification here with userinfo dictionary
}