Push Notifications in iOS 10

The UserNotifications framework(UserNotifications.framework) supports the delivery and handling of local and remote notifications.(The new framework introduced with iOS 10 SDK. )
//*Steps for implement code to handle push notifications in iOS 10
*Import UserNotifications.framework in your AppDelegate file

#import <UserNotifications/UserNotifications.h>

**Add UNUserNotificationCenterDelegate.
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
**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.
As per my opinion you have to set the deployment target to iOS 8 or iOS 9 and later. 
Define constant for version check :
#import "AppDelegate.h"
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) 
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self registerForRemoteNotification];
    
    return YES;
}
- (void)registerForRemoteNotification {

    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 {
        [[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}

***There are two delegate methods need to be handled :
#pragma mark - UNUserNotificationCenter Delegate // >= iOS 10
//Be 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(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}
//Called to know which action selectd by user for a given notification.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler{
    
    NSLog(@"User Info = %@",response.notification.request.content.userInfo);
    completionHandler();
}

**Add Push Notifications Entitlements
Go to your project target's Capabilities tab and add Push Notifications Entitlements.
You will See Cloud and Push Notifications Option in Capabilities,Enable Push Notifications.
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.


View My Profile

Comments

Most Visited

iOS Developer Sample Resume

Progress Indicators (Loading...)

Software Test Engineer Sample Resume