How to enable Push Notification for IOS10 using Objective C

Home / Mobile / How to enable Push Notification for IOS10 using Objective C

A month back, I was working on an iPhone app and it was compatible from iOS 8 to latest iOS version iOS10 We need to integrate Push Notification service in the app. Apple has introduced a new framework called “UserNotifications”  with iOS 10 SDK. This framework – UserNotifications.framework is required for Remote Push Notification in iOS10.

For this we need to change/update few lines of code – to make it compatible with iOS 10.

Push Notification will be displayed in the device's Notification Centre.

Push Notification

Steps to implement code to handle push notifications in iOS10


stepOne : Add New Framework

a. Import UserNotifications.framework in your AppDelegate file

#import <UserNotifications/UserNotifications.h>

b. Also, add UNUserNotificationCenterDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

stepTwo : Register for Push Notification

Before registration check the version of iOS and then based on versions do the code. For iOS 7 code was different, fro iOS 8 & 9 code was different and again for iOS 10 code is different.

a. Define constant for version check at the header part of the AppDelegate.m file.

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

b. Get & Store Your Device Token

– (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

{

   NSString *tokenString = [deviceToken description];

   tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”<>”]];

   tokenString = [tokenString stringByReplacingOccurrencesOfString:@” ” withString:@””];

   NSLog(@”Push Notification tokenstring is %@”,tokenString);

   [[NSUserDefaults standardUserDefaults]setObject:tokenString forKey:@”DeviceTokenFinal”];

   [[NSUserDefaults standardUserDefaults]synchronize];

}

c. Add code in your did finish launching

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

    [self registerForRemoteNotifications];

    return YES;
}

– (void)registerForRemoteNotifications {

   if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@”10.0″)){

       UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

       center.delegate = self;

       [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){

           if(!error){

               [[UIApplication sharedApplication] registerForRemoteNotifications];

           }

       }];

   }

   else {

       // Code for old versions

       UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |

                                                       UIUserNotificationTypeBadge |

                                                       UIUserNotificationTypeSound);

       UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes

                                                                                 categories:nil];

       [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

       [[UIApplication sharedApplication] registerForRemoteNotifications];

         }

}

stepThree : Handling delegate methods for UserNotifications

From iOS10, We can display notification when application is in foreground.

//Called when a notification is delivered to a foreground app.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

   NSLog(@”User Info : %@”,notification.request.content.userInfo);

completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);

   [self handleRemoteNotification:[UIApplication sharedApplication] userInfo:notification.request.content.userInfo];

 }

//Called to let your app know which action was selected by the user for a given notification.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

   NSLog(@”User Info : %@”,response.notification.request.content.userInfo);

   completionHandler();

   [self handleRemoteNotification:[UIApplication sharedApplication] userInfo:response.notification.request.content.userInfo];

 }

stepFour : Handle Remote Notification once received

-(void) handleRemoteNotification:(UIApplication *) application   userInfo:(NSDictionary *) remoteNotif {

   NSLog(@”handleRemoteNotification”);

   NSLog(@”Handle Remote Notification Dictionary: %@”, remoteNotif);

  // Handle Click of the Push Notification From Here…
 // You can write a code to redirect user to specific screen of the app here….

}

stepFive : Add Push Notifications Entitlements

Go to
1. Your project target
2. In Capabilities tab
3. Add Push Notifications Entitlements as per below screenshot.

Project Entitlements for enabling Push Service.

Project Entitlements


You will see a file named ProjectName.entitlements in Project Navigator menu.

We will also post on How to enable Push Notification for IOS 10 using Swift.

 

Related Posts
Get in Touch With Us Today!
India Office
  • Techleos
  • +91-9925999544
  • +91-9427900235
  • 324 Baronet Complex, Sabarmati, Ahmedabad, Gujarat(INDIA) - 380005
  • info@techleos.com
  • http://techleos.com
Location