Thursday 16 October 2014

Register For Push Notification in iOS8

Register for Notification

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
 {  
 #ifdef __IPHONE_8_0  
   UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge  
                                              |UIRemoteNotificationTypeSound  
                                              |UIRemoteNotificationTypeAlert) categories:nil];  
   [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
 #else  
   UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;  
   [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];  
 #endif  
   return YES;  
 }  

Delegate Methods for Handle Push Notification


#pragma mark - Push Notification Methods

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  
 {  
   //register to receive notifications  
   [application registerForRemoteNotifications];  
 }

  
 //For interactive notification only  
 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler  
 {  
   //handle the actions  
   if ([identifier isEqualToString:@"declineAction"]){  
   }  
   else if ([identifier isEqualToString:@"answerAction"]){  
   }  
 }

  
 -(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken  
 {  
   NSLog(@“Device token : %@", deviceToken);  
 }

  
 - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error  
 {  
   NSLog(@"Failed to get token, error: %@", error);  
 }

  
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
 {  
   NSLog(@“Your notification Info : %@", userInfo);  
 }

  
 -(void)handleRemoteNitification:(UIApplication *)application userInfo:(NSDictionary *)userInfo  
 {  
   NSLog(@“Your notification Info : %@", userInfo);  
 }