目录

Anonymous Application Identifier (AAID)

Anonymous Application Identifier (AAID) identifies an app instance running on a mobile smart device. This identifier is accessible only to that specific app instance, exists only during the app's installation, and has a total length of 36 characters. Compared with device-level hardware IDs that cannot be reset, AAIDs offer improved privacy.

To be specific, an AAID has the following strengths:

Scenarios where the AAID may change include but are not limited to the following:

You can build user profiles by AAID and bind them to push tokens to implement precise message push. This module provides capabilities for obtaining and deleting AAIDs.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Push.PushService

Since: 4.0.0(10)

Modules to Import

import { AAID } from '@kit.PushKit';

AAID.getAAID

getAAID(callback: AsyncCallback<string>): void

Obtains an AAID. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Push.PushService

Since: 4.0.0(10)

Parameters:

Name Type Mandatory Description
callback AsyncCallback<string> Yes Callback used to return the result. If the operation is successful, err is undefined, and aaid is the obtained AAID, which contains 36 characters. Otherwise, err is an error object.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
1000900001 System internal error.
1000900006 Failed to connect to the AAID service.
1000900007 Internal error of the AAID service.

Example:

import { AAID } from '@kit.PushKit';
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const LOG_DOMAIN = 0x0000;
const LOG_TAG = 'EntryAbility';

export default class EntryAbility extends UIAbility {
  private retryCount = 0;

  private readonly MAX_RETRY_COUNT = 3;

  private readonly RETRY_INTERVAL = 1000; // Retry interval: 1s

  private readonly RETRY_ERROR_CODES = [
    1000900001,
    1000900006,
    1000900007
  ];

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    super.onCreate(want, launchParam);

    this.getAAID();
  }
  
  /**
   * Obtain an AAID.
   */
  private getAAID(): void {
    try {
      AAID.getAAID((err: BusinessError, aaid: string) => {
        if (err) {
          hilog.error(LOG_DOMAIN, LOG_TAG, 'Failed to get AAID: %{public}d %{public}s', err.code, err.message);
          // Retry logic.
          this.handleRetry(err.code);
        } else {
          hilog.info(LOG_DOMAIN, LOG_TAG, 'Succeeded in getting AAID: %{public}s', aaid);
        }
      });
    } catch (err) {
      const e: BusinessError = err as BusinessError;
      hilog.error(LOG_DOMAIN, LOG_TAG, 'Get AAID occur err: %{public}d %{public}s', e.code, e.message);
    }
  }

  /**
   * Retry logic.
   * You can control the retry times and interval based on your service needs.
   * @param errorCode Error code.
   */
  private handleRetry(errorCode: number): void {
    if (this.retryCount < this.MAX_RETRY_COUNT && this.RETRY_ERROR_CODES.includes(errorCode)) {
      this.retryCount++;
      hilog.warn(LOG_DOMAIN, LOG_TAG, 'getAAID retry count %{public}d', this.retryCount);

      // Retry after a delay to avoid frequent API calls.
      setTimeout(() => {
        this.getAAID();
      }, this.RETRY_INTERVAL);
    }
  }
}

AAID.getAAID

getAAID(): Promise<string>

Obtains an AAID. This API uses a promise to return the result.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Push.PushService

Since: 4.0.0(10)

Returns:

Type Description
Promise<string> Promise used to return the AAID that contains 36 characters.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
1000900001 System internal error.
1000900006 Failed to connect to the AAID service.
1000900007 Internal error of the AAID service.

Example:

import { AAID } from '@kit.PushKit';
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const LOG_DOMAIN = 0x0000;
const LOG_TAG = 'EntryAbility';

export default class EntryAbility extends UIAbility {
  private retryCount = 0;

  private readonly MAX_RETRY_COUNT = 3;

  private readonly RETRY_INTERVAL = 1000; // Retry interval: 1s

  private readonly RETRY_ERROR_CODES = [
    1000900001,
    1000900006,
    1000900007
  ];

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    super.onCreate(want, launchParam);

    this.getAAID();
  }

  /**
   * Obtain an AAID.
   */
  private getAAID(): void {
    AAID.getAAID().then((aaid: string) => {
      hilog.info(LOG_DOMAIN, LOG_TAG, 'Succeeded in getting AAID: %{public}s', aaid);
    }).catch((err: BusinessError) => {
      hilog.error(LOG_DOMAIN, LOG_TAG, 'Failed to get AAID: %{public}d %{public}s', err.code, err.message);
      // Retry logic.
      this.handleRetry(err.code);
    });
  }

  /**
   * Retry logic.
   * You can control the retry times and interval based on your service needs.
   * @param errorCode Error code.
   */
  private handleRetry(errorCode: number): void {
    if (this.retryCount < this.MAX_RETRY_COUNT && this.RETRY_ERROR_CODES.includes(errorCode)) {
      this.retryCount++;
      hilog.warn(LOG_DOMAIN, LOG_TAG, 'getAAID retry count %{public}d', this.retryCount);

      // Retry after a delay to avoid frequent API calls.
      setTimeout(() => {
        this.getAAID();
      }, this.RETRY_INTERVAL);
    }
  }
}

AAID.deleteAAID

deleteAAID(callback: AsyncCallback<void>): void

Deletes an AAID. This API uses an asynchronous callback to return the result. You are not advised to call this API. After this API is called, the push token obtained through getToken() will change.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Push.PushService

Since: 4.0.0(10)

Parameters:

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result. If the operation is successful, err is undefined. Otherwise, err is an error object.

Error codes:

For details about the error codes, please refer to Universal Error Codes and ArkTS API Error Codes.

ID Error Message
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
1000900001 System internal error.
1000900006 Failed to connect to the AAID service.
1000900007 Internal error of the AAID service.

Example:

import { AAID } from '@kit.PushKit';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const LOG_DOMAIN = 0x0000;
const LOG_TAG = 'EntryAbility';

export default class EntryAbility extends UIAbility {
  /**
   * Delete the AAID.
   * You can call the deleteAAID API based on your service needs. Do not call this API unless necessary.
   */
  private deleteAAID(): void {
    try {
      AAID.deleteAAID((err: BusinessError) => {
        if (err) {
          hilog.error(LOG_DOMAIN, LOG_TAG, 'Failed to delete AAID: %{public}d %{public}s', err.code, err.message);
        } else {
          hilog.info(LOG_DOMAIN, LOG_TAG, 'Succeeded in deleting AAID.');
        }
      });
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(LOG_DOMAIN, LOG_TAG, 'Delete AAID occur err: %{public}d %{public}s', e.code, e.message);
    }
  }
}

AAID.deleteAAID

deleteAAID(): Promise<void>

Deletes an AAID. This API uses a promise to return the result. You are not advised to call this API. After this API is called, the push token obtained through getToken() will change.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since version 5.0.0(12).

System capability: SystemCapability.Push.PushService

Since: 4.0.0(10)

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
1000900001 System internal error.
1000900006 Failed to connect to the AAID service.
1000900007 Internal error of the AAID service.

Example:

import { AAID } from '@kit.PushKit';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const LOG_DOMAIN = 0x0000;
const LOG_TAG = 'EntryAbility';

export default class EntryAbility extends UIAbility {
  /**
   * Delete the AAID.
   * You can call the deleteAAID API based on your service needs. Do not call this API unless necessary.
   */
  private deleteAAID(): void {
    try {
      AAID.deleteAAID().then(() => {
        hilog.info(LOG_DOMAIN, LOG_TAG, 'Succeeded in deleting AAID.');
      }).catch((err: BusinessError) => {
        hilog.error(LOG_DOMAIN, LOG_TAG, 'Failed to delete AAID: %{public}d %{public}s', err.code, err.message);
      });
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(LOG_DOMAIN, LOG_TAG, 'Delete AAID occur err: %{public}d %{public}s', e.code, e.message);
    }
  }
}