The multi-sender function is supported on EMUI 9.1.0 or later.
Class Info |
|---|
public class HmsInstanceId extends Object Provides methods for obtaining an AAID and a token required for accessing Push Kit. |
Qualifier and Type |
Method Name and Description |
|---|---|
static HmsInstanceId |
getInstance(Context context) Obtains the instance of the HmsInstanceId class. |
String |
getId() Obtains an AAID in synchronous mode. |
getAAID() Obtains an AAID in asynchronous mode. |
|
long |
Obtains the generation timestamp of an AAID. |
void |
Deletes the locally generated AAID and generation timestamp, as well as all tokens related to the AAID. |
String |
getToken() CAUTION:
This method has been deprecated. Instead, use the getToken(String appId, String scope) method. |
String |
getToken(String appId, String scope) Obtains a token required for accessing Push Kit. |
String |
getToken(String subjectId) Obtains a token that a target app developer applies for a sender to integrate Push Kit in the multi-sender scenario. |
void |
deleteToken(String appId, String scope) Deletes a token. |
void |
deleteToken(String subjectId) Deletes a token that a target app developer applies for a sender to integrate Push Kit in the multi-sender scenario. |
Method |
|---|
public static HmsInstanceId getInstance(Context context) Obtains the instance of the HmsInstanceId class. If the instance does not exist, one instance will be created. Other methods such as getId and getToken in this class can be called only after this instance is obtained. |
Parameters
Name |
Description |
|---|---|
context |
App context. |
To integrate the HMS Core SDK of the latest version into an app, the version of HMS Core (APK) must be 3.0.0 or later. If the version is earlier than 3.0.0, for an app that can run in the foreground, the HMS Core SDK displays a message to prompt the user to upgrade HMS Core (APK) to the latest version. In this case, an Activity instance must be passed using an input parameter of getInstance. For an app that only runs in the background, if you do not want to display the upgrade wizard window to the user, a Context instance of a non-Activity type can be passed.
Returns
Type |
Description |
|---|---|
static HmsInstanceId |
An instance. |
Throws
Name |
Description |
|---|---|
NullPointerException |
If the input parameter is set to null. |
Method |
|---|
public String getId() Obtains an AAID in synchronous mode. Before obtaining a token, call this method to obtain an AAID. The Push Kit 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 obtains a token. If the app needs to report statistics events, it must carry the AAID as its unique ID. |
Returns
Type |
Description |
|---|---|
String |
AAID. |
Method |
|---|
public Task<AAIDResult> getAAID() Obtains an AAID in asynchronous mode. You need to add a listener to listen to the operation result. |
Returns
Type |
Description |
|---|---|
An operation result of the Task<AAIDResult> type. You need to add a listener to listen to the operation result. Once the task is successfully executed, you can use the getId() method in AAIDResult to obtain an AAID. |
Java
public void getAAID() {
Task<AAIDResult> idResult = HmsInstanceId.getInstance(context).getAAID();
idResult.addOnSuccessListener(new OnSuccessListener<AAIDResult>() {
@Override
public void onSuccess(AAIDResult aaidResult) {
// Called when the AAID is obtained.
String aaid = aaidResult.getId();
Log.d(TAG, "getAAID success:" + aaid );
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Called when the AAID fails to be obtained.
Log.d(TAG, "getAAID failure:" + e);
}
});
}
Kotlin
fun getAAID() {
val idResult = HmsInstanceId.getInstance(context).aaid
idResult.addOnSuccessListener{ aaidResult ->
// Called when the AAID is obtained.
Log.d(TAG, "getAAID success, the aaid is " + aaidResult.getId())
}.addOnFailureListener{ exception ->
// Called when the AAID fails to be obtained.
Log.d(TAG, "getAAID failed, catch exception: $exception")
}
}
Method |
|---|
public long getCreationTime() Obtains the generation timestamp of an AAID. |
Returns
Type |
Description |
|---|---|
long |
A 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 Deletes the locally generated AAID and generation timestamp, as well as all tokens related to the AAID. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked. |
Throws
Name |
Description |
|---|---|
If this method fails to be called. You can obtain exception information using the methods provided by the ApiException class. |
Java
public void deleteAAID() {
// Create a thread.
new Thread() {
@Override
public void run() {
try {
// Delete the AAID.
HmsInstanceId.getInstance(context).deleteAAID();
Log.i(TAG, "deleteAAID ok.");
} catch (Exception e) {
Log.i(TAG, "deleteAAID failed! Catch exception : " + e);
}
}
}.start();
}
Kotlin
fun deleteAAID() {
// Create a thread.
object : Thread() {
override fun run() {
try {
// Delete the AAID.
HmsInstanceId.getInstance(context).deleteAAID()
Log.i(TAG, "deleteAAID successfully.")
} catch (e: Exception) {
Log.i(TAG, "deleteAAID failed! Catch exception : $e")
}
}
}.start()
}
This method has been deprecated. Instead, use the getToken(String appId, String scope) method.
Method |
|---|
public String getToken() Obtains a token required for accessing Push Kit. |
Method |
|---|
public String getToken(String appId, String scope) throws ApiException Obtains a token required for accessing Push Kit. If there is no local AAID, this method will automatically generate an AAID when it is called because the Push Kit server needs to generate a token based on the AAID. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked. |
Parameters
Name |
Description |
|---|---|
appId |
App ID allocated when an app is created in AppGallery Connect. |
scope |
Authorization scope. Currently, you can pass the constant DEFAULT_TOKEN_SCOPE, that is, set this parameter to HCM (default value of this constant). |
Returns
Type |
Description |
|---|---|
String |
A token required for accessing Push Kit. |
Throws
Name |
Description |
|---|---|
If this method fails to be called. For example, error code 907122050 is thrown when this method is called in the main thread. You can obtain exception information using the methods provided by the ApiException class. |
Method |
|---|
public String getToken(String subjectId) throws ApiException Obtains a token that a target app developer applies for a sender to integrate Push Kit in the multi-sender scenario. The subjectId parameter needs to be passed to the sender project ID in AppGallery Connect. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked. |
Parameters
Name |
Description |
|---|---|
subjectId |
Project ID of the sender. Sign in to AppGallery Connect, select My projects, find your project in the project list, and go to Project settings. On the page that is displayed, obtain the project ID of the sender. |
Returns
Type |
Description |
|---|---|
String |
A token required for accessing Push Kit. |
Throws
Name |
Description |
|---|---|
If this method fails to be called. |
Method |
|---|
public void deleteToken(String appId, String scope) throws ApiException Deletes a token. When this method is called, the token will be deleted, but the AAID will be not. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked. |
Parameters
Name |
Description |
|---|---|
appId |
App ID allocated when an app is created in AppGallery Connect. |
scope |
Authorization scope. Currently, you can pass the constant DEFAULT_TOKEN_SCOPE, that is, set this parameter to HCM (default value of this constant). |
Throws
Name |
Description |
|---|---|
If this method fails to be called. For example, error code 907122050 is thrown when this method is called in the main thread. You can obtain exception information using the methods provided by the ApiException class. |
Method |
|---|
public void deleteToken(String subjectId) throws ApiException Deletes a token that a target app developer applies for a sender to integrate Push Kit in the multi-sender scenario. The subjectId parameter needs to be passed to the sender project ID in AppGallery Connect. This method is a synchronous method. Do not call it in the main thread. Otherwise, the main thread may be blocked. |
Parameters
Name |
Description |
|---|---|
subjectId |
Project ID of the sender. Sign in to AppGallery Connect, select My projects, find your project in the project list, and go to Project settings. On the page that is displayed, obtain the project ID of the sender. |
Throws
Name |
Description |
|---|---|
If this method fails to be called. |