Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.
| CLASS info |
| public class HmsMessaging extends Object |
| This class provides methods for subscribing to topics and other functions. |
| Type | Name | Desc |
| String | DEFAULT_TOKEN_SCOPE | Default value of the scope parameter when the HmsInstanceId.getToken(String appid, String scope) method is called to obtain a registration token. |
| Return type | Method name |
| static synchronized HmsMessaging | getInstance(Context context) Obtains the instance. |
| boolean | Checks whether automatic initialization is enabled. |
| void | setAutoInitEnabled(boolean enable) Determines whether to enable automatic initialization. |
| void | send(RemoteMessage message) Sends upstream messages in asynchronous mode. |
| Task<Void> | subscribe(String topic) Subscribes to topics in asynchronous mode. |
| Task<Void> | unsubscribe(String topic) Unsubscribes from topics in asynchronous mode. |
| Task<Void> | Enables the function of receiving notification messages in asynchronous mode. |
| Task<Void> | Disables the function of receiving notification messages in asynchronous mode. |
| Constant |
| public static final String DEFAULT_TOKEN_SCOPE |
Default value of the scope parameter when the HmsInstanceId.getToken(String appid, String scope) method is called to obtain a registration token. Constant Value: "HCM" |
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. |
Return
| type | desc |
| HmsMessaging | Instance. |
| Method |
| public boolean isAutoInitEnabled() |
| This method is used to check whether automatic initialization is enabled. |
Return
| type | desc |
| boolean | Boolean value. The value true indicates that automatic initialization is enabled and false indicates that automatic initialization is not enabled. |
| Method |
| public void setAutoInitEnabled(boolean enable) |
| This method is used to determine whether to enable automatic initialization. If this parameter is set to true, the SDK automatically generates an AAID and applies for a token. The token is returned through the onNewToken() callback method. |
Parameters
| Parameter name | Parameter desc |
| enable | Indicates whether to enable automatic initialization. The value true indicates yes and false indicates no. |
| Method |
| public void send(RemoteMessage message) |
This method is used to send upstream messages in asynchronous mode. |
Parameters
| Parameter name | Parameter desc |
message | Message to be sent. |
Throws
| Throw name | Throw desc |
| IllegalArgumentException | A mandatory parameter (such as to/message_id/data) is missing in the RemoteMessage message body. |
| Method |
| public Task<Void> subscribe(String topic) |
| This method is used to subscribe to topics in asynchronous mode. The HUAWEI Push Kit topic messaging function allows you to send messages to multiple devices whose users have subscribed to a specific topic. You can write messages about the topic as required, and HUAWEI Push Kit determines the release path and sends messages to the correct devices in a reliable manner.You need to add a listener to listen to the operation result. |
Parameters
| Parameter name | Parameter desc |
| topic | Topic to be subscribed to. The value must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900} |
Throws
| Throw name | Throw desc |
| IllegalArgumentException | Invalid topic name. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900} |
Return
| type | desc |
| Task<Void> | You need to add a listener to listen to the operation result. |
Sample Code
- public void subscribe(String topic) {
- try {
- HmsMessaging.getInstance(context).subscribe(topic)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "subscribe Complete");
- } else {
- Log.e(TAG, "subscribe failed: ret=" + task.getException().getMessage());
- }
- }
- });
- } catch (Exception e) {
- Log.e(TAG, "subscribe failed: exception=" + e.getMessage());
- }
- }
| Method |
| public Task<Void> unsubscribe(String topic) |
| This method is used to unsubscribe from topics that are subscribed to through the subscribe method. You need to add a listener to listen to the operation result. |
Parameters
| Parameter name | Parameter desc |
| topic | Name of a topic to be unsubscribed from. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900} |
Throws
| Throw name | Throw desc |
| IllegalArgumentException | Invalid topic name. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900} |
Return
| type | desc |
| Task<Void> | You need to add a listener to listen to the operation result. |
Sample Code
- public void unsubscribe(String topic) {
- try {
- HmsMessaging.getInstance(context).unsubscribe(topic)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "unsubscribe Complete");
- } else {
- Log.e(TAG, "unsubscribe failed: ret=" + task.getException().getMessage());
- }
- }
- });
- } catch (Exception e) {
- Log.e(TAG, "unsubscribe failed: exception=" + e.getMessage());
- }
- }
| Method |
| public Task<Void> turnOnPush() |
| This method is used to enable the display of notification messages. If you want to control the display of notification messages in an app, you can call this method. This method applies to notification messages but not data messages. It is the app that determines whether to enable or disable data messaging. |
Return
| type | desc |
| Task<Void> | You need to add a listener to listen to the operation result. |
Sample Code
- HmsMessaging.getInstance(context).turnOnPush()
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "turnOnPush Complete");
- } else {
- Log.e(TAG, "turnOnPush failed: cause=" + task.getException().getMessage());
- }
- }
- });
| Method |
| public Task<Void> turnOffPush() |
| This method is used to disable the display of notification messages. If you want to control the display of notification messages in an app, you can call this method. This method applies to notification messages but not data messages. It is the app that determines whether to enable or disable data messaging. |
Return
| type | desc |
| Task<Void> | You need to add a listener to listen to the operation result. |
Sample Code
- HmsMessaging.getInstance(context).turnOffPush()
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(Task<Void> task) {
- if (task.isSuccessful()) {
- Log.i(TAG, "turnOffPush Complete");
- } else {
- Log.e(TAG, "turnOffPush failed: cause =" + task.getException().getMessage());
- }
- }
- });