1

Could anybody explain to me how to register for remote push notification and catch the token, using the new UserNotification framework provided by iOS10 ?

2 Answers2

3

You can register to notifications like you're already doing from iOS 8 (it's one of the few API for notifications that hasn't changed).

First, in the application:didFinishLaunchingWithOptions: method of the AppDelegate, request the authorization for your app:

UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in
    //here you can check the correct authorization    
}

This will show the usual "Application would like to send you notifications" alert. The main improvement of the new requestAuthorization method is that you can manage the behaviour of tapping on Allow / Don't Allow buttons directly in the closures.

Next, register for remote notifications with the registerForRemoteNotifications method of UIApplication available from iOS 8:

UIApplication.shared().registerForRemoteNotifications()

...and finally manage the registration with your notifications server (like Amazon, OneSignal, etc...)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    //if you need the token as a string, do this:
    let tokenString = String(data: deviceToken, encoding: .utf8)

    //call the notifications server for sending the device token
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Application failed to register for remote notifications")
}

Reference

Nicola Giancecchi
  • 3,045
  • 2
  • 25
  • 41
2

With Objective-C method:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [self registerForRemoteNotification];
        . . .
    }


    - (void)registerForRemoteNotification {
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
            UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter];
            [uncenter setDelegate:self];
            [uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound)
                                    completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                        [[UIApplication sharedApplication] registerForRemoteNotifications];
                                        NSLog(@"%@" , granted ? @"success to request authorization." : @"failed to request authorization .");
                                    }];
            [uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings);
                if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
                    //TODO:
                } else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
                    //TODO:
                } else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
                    //TODO:
                }
            }];
        }
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            UIUserNotificationType types = UIUserNotificationTypeAlert |
                                           UIUserNotificationTypeBadge |
                                           UIUserNotificationTypeSound;
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else {
            UIRemoteNotificationType types = UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeAlert |
                                             UIRemoteNotificationTypeSound;
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
        }
    #pragma clang diagnostic pop
    }

Here is a demo: iOS10AdaptationTips.

ChenYilong
  • 8,543
  • 9
  • 56
  • 84