目录

CLASS info
public class HmsInstanceId extends Object
This class provides methods for obtaining the AAID of an app and obtaining tokens required for accessing HUAWEI Push Kit.

Public Method Summary

Return typeMethod name
static HmsInstanceIdgetInstance(Context context)
Obtains the instance of the HmsInstanceId class.
StringgetId()
Obtains an AAID in synchronous mode.
Task<AAIDResult>getAAID()
Obtains an AAID in asynchronous mode.
longgetCreationTime()
Obtains the generation timestamp of an AAID.
voiddeleteAAID()
Deletes a local AAID and its generation timestamp.
StringgetToken()
Obtains a token. This method is not recommended. You are advised to use the getToken(String appId, String scope) method to obtain tokens.
StringgetToken(String appId, String scope)
Obtains a token required for accessing HUAWEI Push Kit.
voiddeleteToken(String appId, String scope)
Deletes a token.

Public Methods

Method
public static HmsInstanceId getInstance(Context context)
This method is used to obtain the instance of the HmsInstanceId class. If the instance does not exist, it will be created. Other methods such as getId and getToken in this class can be called only after this instance is obtained.

Parameters

Parameter nameParameter desc
contextApp context. To integrate HMS SDK of the latest version with an app, the version of HMS Core (APK) in the app must be 3.0.0 or later. If the version is earlier than 3.0.0, for an app that has a foreground page, HMS SDK displays a message to prompt users to upgrade HMS Core (APK) to the latest version. In this case, an Activity class instance must be transferred as an input parameter of getInstance. For an app that only runs in background, if the developer does not want to display the upgrade wizard window to users, a non-Activity Context class instance can be transferred.

Throws

Throw nameThrow desc
NullPointerExceptionException that occurs when an input parameter is an empty reference.


Return

typedesc
static HmsInstanceIdInstance.
Method
public String getId()
This method is used to obtain an AAID in synchronous mode. Before applying for a token, an app calls this method to obtain its unique AAID. The HUAWEI Push server generates a token for the app based on the AAID. If the AAID of the app changes, a new token will be generated next time when the app applies for a token. If an app needs to report statistics events, it must carry the AAID as its unique ID.

Return

typedesc
StringAAID of an app.
Method
public Task<AAIDResult> getAAID()
This method is used to obtain an AAID in asynchronous mode. You need to add a listener to listen to the operation result.

Return

typedesc
Task<AAIDResult>You need to add a listener to listen to the operation result. If the operation is successful, you can obtain the AAID using the AAIDResult.getId() method.

Sample Code

  1. public void t01_getAAID() {
  2.    Task<AAIDResult> idResult = HmsInstanceId.getInstance(context).getAAID();
  3.    idResult.addOnSuccessListener(new OnSuccessListener<AAIDResult>() {
  4.        @Override
  5.        public void onSuccess(AAIDResult aaidResult) {
  6.            String aaid = aaidResult.getId();
  7.            Log.d(TAG, "getAAID success:" + aaid );
  8.        }
  9.    }).addOnFailureListener(new OnFailureListener() {
  10.        @Override
  11.        public void onFailure(Exception e) {
  12.            Log.d(TAG, "getAAID failure:" + e);
  13.        }
  14.    });
  15. }
Method
public long getCreationTime()
This method is used to obtain the generation timestamp of an AAID.

Return

typedesc
longTimestamp, which is the number of milliseconds from 00:00:00 on January 1, 1970 to the AAID generation time.
Method
public void deleteAAID() throws ApiException
This method is used to delete a local AAID and its generation timestamp.

Throws

Throw nameThrow desc
ApiExceptionAll exceptions that occur when this method fails to be called are thrown through the ApiException class. You can obtain exception information using the methods provided by the ApiException class.

Sample Code

  1. public void t05_deleteAAID() {
  2.    new Thread() {
  3.        @Override
  4.        public void run() {
  5.            try {
  6.                HmsInstanceId.getInstance(context).deleteAAID();
  7.                Log.i(TAG, "deleteAAID ok.");
  8.            } catch (Exception e) {
  9.                Log.i(TAG, "deleteAAID failed!");
  10.            }
  11.        }
  12.    }.start();
  13. }
Method
public String getToken()
Notice:
This method is not recommended. You are advised to use the getToken(String appId, String scope) method to obtain tokens.

Return

typedesc
StringToken required for accessing HUAWEI Push Kit.
Method
public String getToken(String appId, String scope) throws ApiException
This method is used to obtain a token required for accessing HUAWEI Push Kit. If there is no local AAID, this method will automatically generate an AAID when it is called because the HUAWEI Push Kit server needs to generate a token based on the AAID. This method is a synchronous method, and you cannot call it in the main thread. Otherwise, the main thread may be blocked.

Parameters

Parameter nameParameter desc
appIdApp ID allocated when an app is created in AppGallery Connect.
scopeAuthorization scope. Currently, you can transfer the constant com.huawei.hms.push.HmsMessaging.DEFAULT_TOKEN_SCOPE, that is, set this parameter to "HCM" (default value of this constant).


Throws

Throw nameThrow desc
ApiExceptionAll exceptions that occur when this method fails to be called are thrown through the ApiException class. For example, ErrorEnum.ERROR_MAIN_THREAD is thrown when this method is called in the main thread. You can obtain exception information using the methods provided by the ApiException class.


Return

typedesc
StringToken required for accessing HUAWEI Push Kit.

Sample Code

  1. public void sync_getToken() {
  2.    new Thread() {
  3.        @Override
  4.        public void run() {
  5.            try {
  6.                String getToken = HmsInstanceId.getInstance(context).getToken(appid, "HCM");
  7.                Log.d(TAG, "getToken:" + getToken);
  8.            } catch (Exception e) {
  9.                Log.i(TAG, "getToken failed.");
  10.            }
  11.        }
  12.    }.start();
  13. }
Method
public void deleteToken(String appId, String scope) throws ApiException
This method is used to delete a token. After a token is deleted, the corresponding AAID will not be deleted. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked.

Parameters

Parameter nameParameter desc
appIdApp ID allocated when an app is created in AppGallery Connect.
scopeAuthorization scope. Currently, you can transfer the constant com.huawei.hms.push.HmsMessaging.DEFAULT_TOKEN_SCOPE, that is, set this parameter to "HCM" (default value of this constant).


Throws

Throw nameThrow desc
ApiExceptionAll exceptions that occur when this method fails to be called are thrown through the ApiException class. For example, ErrorEnum.ERROR_MAIN_THREAD is thrown when this method is called in the main thread. You can obtain exception information using the methods provided by the ApiException class.

Sample Code

  1. public void t06_deleteToken() {
  2.    new Thread() {
  3.        @Override
  4.        public void run() {
  5.            try {
  6.                HmsInstanceId.getInstance(context).deleteToken(appid, "HCM");
  7.                Log.i(TAG, "deleteToken ok.");
  8.            } catch (Exception e) {
  9.                Log.i(TAG, "deleteToken failed.");
  10.            }
  11.        }
  12.    }.start();
  13. }