| 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. |
| Return type | Method name |
| static HmsInstanceId | getInstance(Context context) Obtains the instance of the HmsInstanceId class. |
| String | getId() Obtains an AAID in synchronous mode. |
| Task<AAIDResult> | getAAID() Obtains an AAID in asynchronous mode. |
| long | getCreationTime() Obtains the generation timestamp of an AAID. |
| void | deleteAAID() Deletes a local AAID and its generation timestamp. |
| String | getToken() Obtains a token. This method is not recommended. You are advised to use the getToken(String appId, String scope) method to obtain tokens. |
| String | getToken(String appId, String scope) Obtains a token required for accessing HUAWEI Push Kit. |
| void | deleteToken(String appId, String scope) Deletes a token. |
| 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 name | Parameter desc |
| context | App 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 name | Throw desc |
| NullPointerException | Exception that occurs when an input parameter is an empty reference. |
Return
| type | desc |
| static HmsInstanceId | Instance. |
Return
| type | desc |
| String | AAID 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
| type | desc |
| 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
- public void t01_getAAID() {
- Task<AAIDResult> idResult = HmsInstanceId.getInstance(context).getAAID();
- idResult.addOnSuccessListener(new OnSuccessListener<AAIDResult>() {
- @Override
- public void onSuccess(AAIDResult aaidResult) {
- String aaid = aaidResult.getId();
- Log.d(TAG, "getAAID success:" + aaid );
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- Log.d(TAG, "getAAID failure:" + e);
- }
- });
- }
| Method |
| public long getCreationTime() |
| This method is used to obtain the generation timestamp of an AAID. |
Return
| type | desc |
| long | Timestamp, 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 name | Throw desc |
| ApiException | All 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
- public void t05_deleteAAID() {
- new Thread() {
- @Override
- public void run() {
- try {
- HmsInstanceId.getInstance(context).deleteAAID();
- Log.i(TAG, "deleteAAID ok.");
- } catch (Exception e) {
- Log.i(TAG, "deleteAAID failed!");
- }
- }
- }.start();
- }
| 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
| type | desc |
| String | Token required for accessing HUAWEI Push Kit. |
Parameters
| Parameter name | Parameter desc |
| appId | App ID allocated when an app is created in AppGallery Connect. |
| scope | Authorization 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 name | Throw desc |
| ApiException | All 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
| type | desc |
| String | Token required for accessing HUAWEI Push Kit. |
Sample Code
- public void sync_getToken() {
- new Thread() {
- @Override
- public void run() {
- try {
- String getToken = HmsInstanceId.getInstance(context).getToken(appid, "HCM");
- Log.d(TAG, "getToken:" + getToken);
- } catch (Exception e) {
- Log.i(TAG, "getToken failed.");
- }
- }
- }.start();
- }
Parameters
| Parameter name | Parameter desc |
| appId | App ID allocated when an app is created in AppGallery Connect. |
| scope | Authorization 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 name | Throw desc |
| ApiException | All 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
- public void t06_deleteToken() {
- new Thread() {
- @Override
- public void run() {
- try {
- HmsInstanceId.getInstance(context).deleteToken(appid, "HCM");
- Log.i(TAG, "deleteToken ok.");
- } catch (Exception e) {
- Log.i(TAG, "deleteToken failed.");
- }
- }
- }.start();
- }