Skip to main content

Push Notifications in iOS 10 [Objective-C]


Push Notification iOS10

1. Push Notification Diagram:



2. Push Notification Diagram:



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. 1
    #import <UserNotifications/UserNotifications.h>
    Also add UNUserNotificationCenterDelegate.
    1
    2
    3
    4
    #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 :
    1
    #define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    Add code in your did finish launching
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    -(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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //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.

All Done!

Comments

Popular posts from this blog

Understanding of Size Classes?

Size Classes are an abstraction of how a device should be categorized depending on its screen dimensions. Apple defined two categorizations for both vertical and horizontal sizes called “Regular” and “Compact”. The former specifies a big space, while the latter specifies a small” space. How big or small exactly? Here, “big” and “small” are not intended to be measured in inches. iOS Size Classes Apple has introduced many devices like iPhone, iPad with different screen sizes and resolution. Also after iOS 8 apple has supported multitasking in iPad. So for the developers to develop a common or single UI for all the devices apple has introduced the concept of an adaptive layout by combining auto layout and size classes. What is adaptive layout: The adaptive layout is a method of building the apps based on the size and characteristics of the container instead of a targeting a particular device. We can create a single layout to work on all...

The differences between Core Data and a Database

Database Core Data Primary function is storing and fetching data Primary function is graph management (although reading and writing to disk is an important supporting feature) Operates on data stored on disk (or minimally and incrementally loaded) Operates on objects stored in memory (although they can be lazily loaded from disk) Stores "dumb" data Works with fully-fledged objects that self-manage a lot of their behavior and can be subclassed and customized for further behaviors Can be transactional, thread-safe, multi-user Non-transactional, single threaded, single user (unless you create an entire abstraction around Core Data which provides these things) Can drop tables and edit data without loading into memory Only operates in memory Perpetually saved to disk (and often crash resilient) Requires a save process Can be slow to create millions of new rows Can create millions of new objects in-memory very quickly (although saving these objects will be slow) Offer...

What Is a Closure?

Closures are self contained chunks of code that can be passed around and used in your code. Closures can capture and store references to any constants or variables from the context in which they are defined. This is know as closing over those variables, hence the name closures. Closures are use intensively in the Cocoa frameworks – which are used to develop iOS or Mac applications. Functions are a special kind of closures. There are three kinds of closures: global functions  – they have a name and cannot capture any values nested functions  – they have a name and can capture values from their enclosing functions closure expressions  – they don’t have a name and can capture values from their context The thing to keep in mind for the moment is that you already have an intuition about closures. They are almost the same as functions but don’t necessarily have a name. // a closure that has no parameters and return a String var hello: () -> ( String ) = { ...