This module checks and manages the status of an app on a device, including determining whether the app is installed for the first time on the device or whether the user has obtained a coupon on the device, to support new user acquisition campaigns.
Since: 5.0.0(12)
getDeviceToken(): Promise<string>
Obtains the device token of the current device. This API uses a promise to return the result.
This API involves device-cloud synergy and time-consuming operations such as network connection. Therefore, do not execute this API in the UI thread, in order to avoid UI thread blocking.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since API version 5.0.2(14).
System capability: SystemCapability.Security.DeviceCertificate
Since: 5.0.0(12)
Returns:
| Type | Description |
|---|---|
| Promise<string> | Promise used to return the device token of the current device. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | has no permission. |
| 1003300005 | internal error. Possible causes: 1. IPC communication failed; 2. Memory operation error; 3. Access device certificate failed. |
| 1003300006 | access cloud server fail. |
Example:
import { deviceCertificate } from '@kit.DeviceSecurityKit';
import { BusinessError} from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const TAG = "DeviceCertificateJsTest";
// Request the device token and process the result.
try {
deviceCertificate.getDeviceToken().then((token) => {
hilog.info(0x0000, TAG, 'Succeeded in executing getDeviceToken');
// Process the device token.
}).catch((err: BusinessError) => {
hilog.error(0x0000, TAG, 'getDeviceToken failed! %{public}d %{public}s', err.code, err.message);
});
} catch (err) {
let error: BusinessError = err as BusinessError;
hilog.error(0x0000, TAG, 'getDeviceToken failed! %{public}d %{public}s', error.code, error.message);
}