The topic messaging function provided by HUAWEI Push Kit allows you to send messages to multiple devices whose users have subscribed to a specific topic. You can write notification messages about the topic as required, and HUAWEI Push Kit sends the messages to correct devices in a reliable manner.
For example, users of a weather forecast app can subscribe to the weather topic and receive notification messages about the best weather for exterminating pests. Users who like travelling can subscribe to travel information such as travel routes and travel guides.
Note the following items about topic messaging:
l Topic messaging is best suited for delivering weather or other information that can be obtained publicly.
l Topic messaging does not limit the number of subscriptions for each topic. However, HUAWEI Push Kit has the following restrictions:
1. An app instance can subscribe to a maximum of 2000 topics.
2. For Huawei devices running EMUI 10.0 or later, the version of HMS Core (APK) must be 3.0.0 or later. For Huawei devices running an EMUI version earlier than 10.0, the version of HMS Core (APK) must be 4.0.3 or later.
To send messages by topic, complete the following process:
1. Subscribe to a topic for an app. This step can be performed on both the client and server. For details about how to subscribe to topics on the client, please refer to Topic Management on the Client. For details about how to subscribe to topics on the server, please refer to Topic Management on the Server.
2. After subscribing to a topic, you can send messages based on the topic. For details, please refer to Sending Messages by Topic.
An app can subscribe to any existing topics or create topics. When an app subscribes to a topic that does not exist, the system creates a topic with the name in HUAWEI Push Kit. Any app can then subscribe to this topic.
To subscribe to a topic, the app needs to use the subscribe method of the HMS Core Push SDK. This method returns a Task that the completion listener can use to determine whether the subscription is successful.
- 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());
- }
- }
To unsubscribe from a topic, the app needs to use the unsubscribe method of the HMS Core Push SDK.
- 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());
- }
- }
The HUAWEI Push Kit server provides basic APIs for topic management. A maximum of 1000 tokens can be transferred for subscribing to or unsubscribing from a topic at a time. You can create a maximum of 2000 topics. After the subscription is complete, wait one minute for the subscription to take effect. Then you can specify one topic or a set of topic matching conditions to send messages in batches.
To implement topic management on your app server, complete the following process:
1. Your app server obtains an access token. For details, please refer to Open Platform Authentication - Client Password Mode.
2. Your app server calls the topic subscription or unsubscription API of the HUAWEI Push Kit server to subscribe to or unsubscribe from a topic. For details about the APIs, please refer to Subscribing to a Topic and Unsubscribing from a Topic.
Sample code of calling the topic subscription API:
URL for calling the API using HTTPS POST: https://push-api.cloud.huawei.com/v1/[appid]/topic:subscribe
Request message header:
Authorization: Bearer CV0kkX7yVJZcTi1i+uk...Kp4HGfZXJ5wSH/MwIriqHa9h2q66KSl5
Content-Type: application/json
Request message body:
- {
- "topic": "weather",
- "tokenArray": [
- "AOffIB70WGIqdFJWJvwG7SOB...xRVgtbqhESkoJLlW-TKeTjQvzeLm8Up1-3K7",
- "AKk3BMXyo80KlS9AgnpCkk8l...uEUQmD8s1lHQ0yx8We9C47yD58t2s8QkOgnQ"
- ]
- }
Sample code of calling the topic unsubscription API:
URL for calling the API using HTTPS POST: https://push-api.cloud.huawei.com/v1/[appid]/topic:unsubscribe
Request message header:
Authorization: Bearer CV0kkX7yVJZcTi1i+uk...Kp4HGfZXJ5wSH/MwIriqHa9h2q66KSl5
Content-Type: application/json
Request message body:
- {
- "topic": "weather",
- "tokenArray": [
- "AOffIB70WGIqdFJWJvwG7SOB...xRVgtbqhESkoJLlW-TKeTjQvzeLm8Up1-3K7",
- "AKk3BMXyo80KlS9AgnpCkk8l...uEUQmD8s1lHQ0yx8We9C47yD58t2s8QkOgnQ"
- ]
- }
After creating a topic, you can send messages based on the topic. You can develop the functions by referring to Server Development Guide. If you develop HTTP-based message sending, the topic parameter in the message structure Message to be sent must be set to a topic.
Sample code of a topic-based messaging request sent to the HUAWEI Push Kit server:
- {
- "validate_only": false,
- "message": {
- "notification": {
- "title": "message title",
- "body": " message body"
- },
- "android": {
- "notification": {
- "click_action": {
- "type": 1,
- "action": "com.huawei.codelabpush.intent.action.test"
- }
- }
- },
- "topic": "weather"
- }
- }
In addition, HUAWEI Push Kit allows you to send messages by topic combinations specified by condition. For the sample code, please refer to Message Sending by Conditions. If you develop HTTP-based message sending, the condition parameter must be specified in the Message structure. For details about the condition expression specifications, please refer to the condition description in Sending Messages.