iOS 10 compatibility with notifications.

Step 1 : 

In your Appdelegate.h file

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>

#else

@interface AppDelegate : UIResponder <UIApplicationDelegate>

#endif

Step 2 : 

In your Appdelegate.m file

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

// other code

  NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];

    if([standardDefaults objectForKey:@”NotificationDeviceToken”]) {

        // Register the supported interaction types.

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000

        {

            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

            center.delegate = self;

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

                if( !error ){

                    [[UIApplication sharedApplication] registerForRemoteNotifications];

                }

            }];

        }

#else

        {

            UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

            // Register for remote notifications.

            [[UIApplication sharedApplication] registerForRemoteNotifications];

        }

#endif

    }

return YES;

}

– (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {

        NSLog( @”===%s===Notification: %@”,__FUNCTION__, userInfo );

    if (application.applicationState != UIApplicationStateActive)

    {

        // Action to be performed when you recieve push notifications.

    }

}

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

    

    DLog( @”===%s===Notification: %@”,__FUNCTION__, userInfo );

    [Crittercism leaveBreadcrumb:[NSString stringWithFormat:@”%s”, __FUNCTION__]];

    // iOS 10 will handle notifications through other methods

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@”10.0″))

    {

        NSLog( @”iOS version >= 10. Let NotificationCenter handle this one.” );

        // set a member variable to tell the new delegate that this is background

        return;

    }

    

    if (application.applicationState != UIApplicationStateActive)

    {

        // Action to be performed when you recieve push notifications.

        completionHandler( UIBackgroundFetchResultNewData );

    }

}

#pragma mark iOS 10 UserNotification delegates

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000

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

    

    NSLog( @”Handle push from foreground – %s=======Notification:%@”,__FUNCTION__,notification.request.content.userInfo);

    // custom code to handle push while app is in the foreground

    // Action to be performed when you recieve push notifications.

}

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

   

    NSLog( @”Handle push from background or closed – %s=======Notification:%@”,__FUNCTION__,response.notification.request.content.userInfo);    

    // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background

           // Action to be performed when you recieve push notifications.

}

#endif

Leave a comment