Nowadays, there are more and more intelligent terminal apps, and the amount of information pushed by each App is rapidly expanding. How to make the information pushed by yourself accurately catch the attention of users and occupy the high ground in the market competition? One of the answers is the topic subscription push.

Theme subscription message push can be based on users’ habits or let users customize the topics they are interested in, and then the app writes theme messages according to needs. The push service is responsible for delivering the messages reliably to the correct devices, so as to achieve accurate push.

For example, users of a weather forecasting app can opt in to the “Weather in X City” topic and receive notifications about local weather.

Huawei theme subscription message push service is stable, timely and efficient. Various push styles, automatic push notifications and Deep linking can help the application accurately reach users and effectively improve user activity and viscosity.

Next, let’s analyze how to integrate huawei topic subscription message push service in detail.

I. Overall development process

Step1: App App theme subscription;

Step2: Send messages to the users who subscribe to the topic;

Step3: Verify that the topic subscription message arrived successfully.

Service side topic subscription push message interaction flowchart

There are two ways to implement topic subscription: App client topic subscription and server topic subscription. This article details the implementation steps and code for each of these subscriptions.

Ii. Key steps of integration and code implementation

(1) Client subject subscription

Client subject subscription code implementation:

public void subtopic(View view) { String SUBTAG = "subtopic"; String topic = "weather"; Try {// subscribe to hmsmessaging.getInstance (pushClient.this).subscribe(topic).addonCompletelistener (new) OnCompleteListener<Void>() { @Override public void onComplete(Task<Void> task) { if (task.isSuccessful()) { Log.i(SUBTAG, "subscribe topic weather successful"); } else { Log.e(SUBTAG, "subscribe topic failed,return value is" + task.getException().getMessage()); }}}); } catch (Exception e) { Log.e(SUBTAG, "subscribe faied,catch exception:" + e.getMessage()); }}Copy the code

App-end test custom subscription interface:

Topic subscription cancellation code implementation:

public void unsubtopic(View view) { String SUBTAG = "unsubtopic"; String topic = "weather"; Try {// subscribe to hmsmessaging.getInstance (pushClient.this).unsubscribe(topic).addonCompletelistener (new) OnCompleteListener<Void>() { @Override public void onComplete(Task<Void> task) { if (task.isSuccessful()) { Log.i(SUBTAG, "unsubscribe topic successful"); } else { Log.e(SUBTAG, "unsubscribe topic failed,return value is" + task.getException().getMessage()); }}}); } catch (Exception e) { Log.e(SUBTAG, "subscribe faied,catch exception:" + e.getMessage()); }}Copy the code

App-end test custom unsubscribe interface:

(2) Server subject subscription

1. Obtain the Access Token. Call huawei account server interface (oauth-login.cloud.huawei.com/oauth2/v3/t… Token.

(1) Request message:

POST/oauth2 / v3 / token HTTP / 1.1 Host: oauth-login.cloud.huawei.com the content-type: application/x-www-form-urlencoded grant_type=client_credentials&client_id=<APP ID >&client_secret=<APP secret >Copy the code

(2) Obtain Access Token request message actual demonstration:

2. Subject subscription and unsubscription. The server calls the server-side topic subscription API or topic unsubscription API to subscribe or unsubscribe application topics to realize the management of application topics. The difference between subscribing and unsubscribing is that the interface address is slightly different. The request header and the message body are all the same. The details are as follows:

(1) Subject subscription interface:

Push-api.cloud.huawei.com/v1/\ [appid \…

(2) Subject unsubscribe interface:

Push-api.cloud.huawei.com/v1/\ [appid \…

(3) Example of request header, here Bearer Token is the Access Token obtained in the previous step:

Authorization: Bearer CV0kkX7yVJZcTi1i + UK... Kp4HGfZXJ5wSH/MwIriqHa9h2q66KSl5Content-Type: application/jsonCopy the code

(4) Request message body example:

{    "topic": "weather",    "tokenArray": [        "AOffIB70WGIqdFJWJvwG7SOB...xRVgtbqhESkoJLlW-TKeTjQvzeLm8Up1-3K7",        "AKk3BMXyo80KlS9AgnpCkk8l...uEUQmD8s1lHQ0yx8We9C47yD58t2s8QkOgnQ"    ]}
Copy the code

(5) Actual demonstration of request message:

(3) Sending subject messages

When a topic is created, messages can be sent based on the topic. Currently, HTTPS is supported for sending topic messages. The following is an example packet based on HTTPS:

{    "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"    }}
Copy the code

App client test display received push messages:

Three, topic subscription message notes

  • Client applications can subscribe to any existing topics or create new topics. When the client application subscribing to a new topic name does not yet exist, the push service creates a new topic using that name, which any client can then subscribe to.

  • The push server provides you with a basic theme management API that allows you to subscribe to or unsubscribe from a topic with up to 1000 tokens at a time, and each application can only have a maximum of 2000 different topics.

  • It takes one minute for the subscription relationship between a Topic and a Token to take effect. After the subscription relationship takes effect, messages can be sent in batches by pointing to a Topic or combining Topic conditions.

>> Official website of Huawei Developer Alliance

>> To get the development guide >> to participate in the developer discussion, visit the CSDN community or Reddit community >> To download the demo and sample code, visit Github or Gitee >> To resolve integration issues, visit Stack Overflow

The original link: developer.huawei.com/consumer/cn…

Original author: Pepper