Token management API

更新时间:
复制 MD 格式

This document describes the DeviceToken APIs that the iOS SDK provides to report and retrieve a DeviceToken.

Report a DeviceToken

To use the Alibaba Cloud Mobile Push service, you must report the device's DeviceToken to Alibaba Cloud. First, request push permissions and register with the Apple Push Notification service (APNs). After you obtain the deviceToken in the success callback, report it.

For more information, see Integrate the iOS SDK—Configure APNs push capabilities.

API definition

+ (void)registerDevice:(NSData *)deviceToken
          withCallback:(CallbackHandler)callback;

Parameters

Parameter

Type

Required

Description

deviceToken

NSData

Yes

The deviceToken returned by the APNs server.

callback

Block

No

The callback function for the registration result.

Examples

// Request user authorization
func setupAPNs() {
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        DispatchQueue.main.async {
            if granted {
                UIApplication.shared.registerForRemoteNotifications()
            }
            print("Push permission status: \(granted ? "Granted" : "Denied")")
        }
    }
}

// Callback for successful APNs device registration
func application(_ application: UIApplication,
               didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    CloudPushSDK.registerDevice(deviceToken) { result in
        let status = result.success ? "succeeded" : "failed"
        print("DeviceToken reporting \(status)")
    }
}
- (void)setupAPNs {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (granted) {
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }
            NSLog(@"Push permission status: %@", granted ? @"Granted" : @"Denied");
        });
    }];
}

// Callback for successful APNs device registration
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *result) {
        NSString *status = result.success ? @"succeeded" : @"failed";
        NSLog(@"DeviceToken reporting %@", status);
    }];
}

Get a DeviceToken

After you successfully report a DeviceToken, you can use the following API to retrieve the reported DeviceToken.

API definition

+ (NSString *)getApnsDeviceToken;