Register remote notifications ios10 and catch the apn token

微笑、不失礼 提交于 2019-12-23 03:16:39

问题


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


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/38547065/register-remote-notifications-ios10-and-catch-the-apn-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!