Push Notifications in iOS 10 [Objective-C]

Push Notification iOS10
Push Notification iOS10

The new framework called “UserNotifications” is introduced with iOS 10 SDK. The UserNotifications framework (UserNotifications.framework) supports the delivery and handling of local and remote notifications.

So, Let see what we have to change to get the push notifications in iOS 10.

Steps for implement code to handle push notifications in iOS 10

  1. Import UserNotifications.framework in your AppDelegate file
  2. #import <UserNotifications/UserNotifications.h>
    

    Also add UNUserNotificationCenterDelegate.

    #import <UserNotifications/UserNotifications.h>
    @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
    
    @end
    

  3. Register for push notification
  4. 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.

    As per my opinion you have to set the deployment target to iOS 8 or iOS 9 and later. For this you can check the adoption ratio of iOS in the devices.

    Define constant for version check :

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

    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
        }
    }
    

  5. Handling delegate methods for UserNotifications
  6. You will be surprise that notification displayed when application in foreground too in iOS 10. As we know that in old versions we display alert or something else which will be look like notification comes in foreground.

    There are two delegate methods need to be handled :

    
    //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);
    }
    
    //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();
    }
    

  7. Add Push Notifications Entitlements
  8. Go to your project target’s Capabilities tab and add Push Notifications Entitlements.

    Push Notifications Entitlements
    Push Notifications Entitlements

    If it’s available in your certificates then it will enable directly else configure your profile with the certificates and you can enable this capability by that.

All Done!

Happy Coding 🙂

I have added sample code of objective-c on github with iOS 8, 9 and 10 support of push notification.
I have also posted about Push Notifications in iOS 10 [Swift].

  • Karthi Keyan

    Thanks a lot..Easy to understand, Your explanation really amazing

    • Ashish Kakkad

      Thanks!

  • Martin

    didRegisterForRemoteNotificationsWithDeviceToken should still be called when using it the iOS 10 way right? Because it is not called for me when I try this.

  • Kamal Bhatt

    Hi Ashish, Thanks for the tutorial, I am not able to get device token, i am using iOS 10 Device, even didRegisterForRemoteNotificationsWithDeviceToken can not call. Please help me on this.

    • Sandy’s Kingdom

      – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      // Override point for customization after application launch.

      if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
      {

      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];
      }
      }];
      }
      }

      – (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
      {
      NSLog(@”My token is: %@”, deviceToken);
      NSString * deviceTokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @”” withString: @””] stringByReplacingOccurrencesOfString: @” ” withString: @””];
      NSLog(@”the generated device token string is : %@”,deviceTokenString);

      This will help you

  • Mohammad Tahir

    not getting device token on ios 10

  • Hareesh Thirunahari

    Thanks a lot man.. This one really helped me. Cheers! 🙂

  • Muhammad Waqas Rafeeq

    Hi Ashish,my app receive notification perfectly on ios 9 but i try ur code its not worked for me… still app not receiving push notification on ios 10…
    could you please tell me what the actual problem is this…??