Topic subscription message push enables users to no longer miss important information

Posted by k3Bobos on Tue, 08 Feb 2022 04:49:13 +0100

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

The topic subscription message push can customize the topics of interest according to the user's habits or allow the user to customize the topics. The application then writes the topic message according to the needs. The push service is responsible for reliably transmitting the message to the correct device, so as to achieve accurate push. For example, users of a weather forecast application can choose to join the "X city weather" theme and receive notifications about local weather.

Huawei's theme subscription message push service has the characteristics of stability, timeliness and efficiency. A variety of push styles, automatic push notifications and Deep linking can help applications accurately reach users and effectively improve user activity and viscosity.

Next, we will analyze in detail how to integrate Huawei topic subscription message push service.

1, Overall development process

Step 1: App theme subscription;
Step 2: Send a message to the user group subscribing to the topic;
Step 3: verify that the topic subscription message is delivered successfully.

Flow chart of service side topic subscription push message interaction

There are two ways to implement topic subscription: App client topic subscription and server topic subscription. This paper will introduce the implementation steps and codes of these two subscription methods in detail.

2, Description of key integration steps and code implementation

(1) Client topic subscription

Client topic subscription code implementation:

public void subtopic(View view) {
    String SUBTAG = "subtopic";
    String topic = "weather";
    try {
        // Topic subscription
    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());
    }
}

User defined subscription interface on App side:

Implementation code of topic subscription cancellation:

public void unsubtopic(View view) {
    String SUBTAG = "unsubtopic";
    String topic = "weather";
    try {
        // Topic subscription
        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());
    }
}

User defined unsubscribe interface for App end test:

(2) Server side topic subscription

1. Get Access Token. Call the interface of Huawei account server( https://oauth-login.cloud.huawei.com/oauth2/v3/token ), obtain the Access Token of the application level authentication certificate.

(1) Request message:

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

(2) Practical demonstration of obtaining Access Token request message:

2. Subject subscription and unsubscribe. The server calls the server-side topic subscription API or topic unsubscribe API to subscribe or unsubscribe to the application topic, so as to realize the management of the application topic. The difference between subject subscription and unsubscribe is that the interface address is slightly different. The request header and message body are always, as follows:

(1) Topic subscription interface:

https://push-api.cloud.huawei.com/v1/[appid]/topic:subscribe

(2) Topic unsubscribe interface:

https://push-api.cloud.huawei.com/v1/[appid]/topic:unsubscribe

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

Authorization: Bearer CV0kkX7yVJZcTi1i+uk...Kp4HGfZXJ5wSH/MwIriqHa9h2q66KSl5
Content-Type: application/json

(4) Example of request message body:

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

(5) Practical demonstration of request message:

(3) Send subject message

After creating a topic, you can send messages according to the topic. Currently, topic message sending based on HTTPS protocol is supported. The example message based on HTTPS protocol is as follows:

{
    "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"
    }
}

App client test shows the push message received:

3, Topic subscription message considerations

The client application can subscribe to any existing topic or create a new topic. When the new topic name subscribed by the client application does not exist, the push service will create a new topic with this name, and then any client can subscribe to the topic.

The push server provides you with a basic topic management API, which supports the subscription or unsubscribe of a topic with less than 1000 tokens at a time. At the same time, each application can only have 2000 different topics at most.

The subscription relationship between Topic and Token takes one minute to take effect. After the subscription relationship takes effect, you can send messages in batches by pointing to Topic or combining Topic conditions.

>>Visit the official website of Huawei push service to learn more about it
>>Obtain Huawei push service development guidance document
>>Huawei HMS Core official forum
>>Huawei push service open source warehouse address: GitHub,Gitee

Click the attention on the right side of the avatar in the upper right corner to learn the latest technology of Huawei mobile services for the first time~

 

Topics: Design Pattern