How to integrate Push Notification in iOS – Tutorial

Push Notification iOS 10 Tutorial

Integrate Push Notification in iOS :

In this post we will learn how to use Push notification in iOS 10. With iOS 10 there are lots of changes push notification implementation. The new framework called “UserNotifications.framework” will supports the delivery and handling of push notification in iOS 10 and onward. With iOS 10, we can also display notification banner while user is using app.

Implementation of code for push notification

Follow below steps to implement code to handle push notification in iOS 10.

Step 1: Import UserNotifications.framework in AppDelegate.m file

#import < UserNotifications/UserNotifications.h >
 

Step 2: Add UNUserNotificationCenterDelegate, telling our class that we are going to implement UNUserNotificationDelegates

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

 
@end

Step 3: Register for push notification in order to get device token. If your app target to iOS below iOS 10 then you have to place check to handle it.

 
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@”10.0″)){
        
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        
        center.delegate = self;
        
        [center requestAuthorizationWithOptions🙁UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadgecompletionHandler:^(BOOL granted, NSError * _Nullable error){
            
            if(!error){
                
                [[UIApplication sharedApplicationregisterForRemoteNotifications];
                
            }
            
        }]; 
        
    }
    else {
 
            //for iOS8

}
 
Note: SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) is the constant as given below
 
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Step 4: It’s time to implement delegate methods of UNUserNotificationDelegates
 
//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();
    
}
 


Step 5: Add push notification entitlements. This will get configured if you properly set up the profile for the push notification capability.Below is the image that showed from where it will gets automatically added to your app.

Turn-on-Push-notification-capabilities-for teh-app-that-will-add-entitlements-file-automatically