We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core ReferencesPush KitAndroid Client APIs (Archived)com.huawei.hms.push (Archived)HmsMessaging (Archived)

HmsMessaging

CLASS info
public class HmsMessaging extends Object
This class provides methods for subscribing to topics and other functions.

Constant Summary

TypeNameDesc
StringDEFAULT_TOKEN_SCOPEDefault value of the scope parameter when the HmsInstanceId.getToken(String appid, String scope) method is called to obtain a registration token.

Public Method Summary

Return typeMethod name
static synchronized HmsMessaging

getInstance(Context context)

Obtains the instance.

boolean

isAutoInitEnabled()

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>

turnOnPush()

Enables the function of receiving notification messages in asynchronous mode.

Task<Void>

turnOffPush()

Disables the function of receiving notification messages in asynchronous mode.

Constants

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"

Public Methods

Method
public static synchronized HmsMessaging getInstance(Context context)
This method is used to obtain the instance of the HmsMessaging class. Before using the topic subscription function or other function, an app can call other methods in the HmsMessaging class only after obtaining the HmsMessaging instance.

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.


Return

typedesc
HmsMessagingInstance.
Method
public boolean isAutoInitEnabled()
This method is used to check whether automatic initialization is enabled.

Return

typedesc
booleanBoolean 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 nameParameter desc
enableIndicates 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 nameParameter desc

message

Message to be sent.


Throws

Throw nameThrow 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 nameParameter desc
topicTopic to be subscribed to. The value must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900}


Throws

Throw nameThrow desc
IllegalArgumentExceptionInvalid topic name. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900}


Return

typedesc
Task<Void>You need to add a listener to listen to the operation result.

Sample Code

Collapse
Word wrap
Dark theme
Copy code
  1. public void subscribe(String topic) {
  2.    try {
  3.        HmsMessaging.getInstance(context).subscribe(topic)
  4.        .addOnCompleteListener(new OnCompleteListener<Void>() {
  5.            @Override
  6.            public void onComplete(Task<Void> task) {
  7.                if (task.isSuccessful()) {
  8.                    Log.i(TAG, "subscribe Complete");
  9.                } else {
  10.                    Log.e(TAG, "subscribe failed: ret=" + task.getException().getMessage());
  11.                }
  12.            }
  13.        });
  14.    } catch (Exception e) {
  15.        Log.e(TAG, "subscribe failed: exception=" + e.getMessage());
  16.    }
  17. }
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 nameParameter desc
topicName of a topic to be unsubscribed from. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900}


Throws

Throw nameThrow desc
IllegalArgumentExceptionInvalid topic name. The topic name must match the following regular expression: [\u4e00-\u9fa5\w-_.~%]{1,900}


Return

typedesc
Task<Void>You need to add a listener to listen to the operation result.

Sample Code

Collapse
Word wrap
Dark theme
Copy code
  1. public void unsubscribe(String topic) {
  2.    try {
  3.        HmsMessaging.getInstance(context).unsubscribe(topic)
  4.        .addOnCompleteListener(new OnCompleteListener<Void>() {
  5.            @Override
  6.            public void onComplete(Task<Void> task) {
  7.                if (task.isSuccessful()) {
  8.                    Log.i(TAG, "unsubscribe Complete");
  9.                } else {
  10.                    Log.e(TAG, "unsubscribe failed: ret=" + task.getException().getMessage());
  11.                }
  12.            }
  13.        });
  14.    } catch (Exception e) {
  15.        Log.e(TAG, "unsubscribe failed: exception=" + e.getMessage());
  16.    }
  17. }
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

typedesc
Task<Void>You need to add a listener to listen to the operation result.

Sample Code

Collapse
Word wrap
Dark theme
Copy code
  1. HmsMessaging.getInstance(context).turnOnPush()
  2.    .addOnCompleteListener(new OnCompleteListener<Void>() {
  3.        @Override
  4.        public void onComplete(Task<Void> task) {
  5.            if (task.isSuccessful()) {
  6.                Log.i(TAG, "turnOnPush Complete");
  7.            } else {
  8.                Log.e(TAG, "turnOnPush failed: cause=" + task.getException().getMessage());
  9.            }
  10.        }
  11.    });
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

typedesc
Task<Void>You need to add a listener to listen to the operation result.

Sample Code

Collapse
Word wrap
Dark theme
Copy code
  1. HmsMessaging.getInstance(context).turnOffPush()
  2.    .addOnCompleteListener(new OnCompleteListener<Void>() {
  3.        @Override
  4.        public void onComplete(Task<Void> task) {
  5.            if (task.isSuccessful()) {
  6.                Log.i(TAG, "turnOffPush Complete");
  7.            } else {
  8.                Log.e(TAG, "turnOffPush failed: cause =" + task.getException().getMessage());
  9.            }
  10.        }
  11.    });


This page may contain third-party content. For details, click here.