[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812131138.45750438755216078185065637565616:50510825011752:2800:033E0C3A908D52CDAA253ECB50F3952C5F8C9F50C695CB2162B4FC7941574BD8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812131138.45750438755216078185065637565616:50510825011752:2800:033E0C3A908D52CDAA253ECB50F3952C5F8C9F50C695CB2162B4FC7941574BD8.png[/IMG]

Huawei Push Kit is a messaging service provided by Huawei for developers. It establishes a communication channel between the cloud and devices. Using HMS Push Kit, developers can send the latest messages to users. This helps developers to maintain closer ties with users and increases user awareness, and activity.

User can tap the message displayed in the notification bar of the device and can open the corresponding app, and view more details.

HMS Push Kit is already available for more than 200+ countries and regions. It offers the capacity of sending 10 million messages per second from the server, delivering 99% of them and providing real time push reports, ultimately helping to improve the DAU of your apps.

In this article we will learn how to send notification using topic-based message sending and how to open a specific page of an app when user taps the notification using Huawei AGC console.

Demo

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812132204.49589434222997139278629172876871:50510825011752:2800:153C64059280D7E06556070C1DB37C6C0D30163BF5A39593CE405D19E983588D.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812132204.49589434222997139278629172876871:50510825011752:2800:153C64059280D7E06556070C1DB37C6C0D30163BF5A39593CE405D19E983588D.gif[/IMG]

Prerequisite

1) Must have a Huawei Developer Account.

2) Must have a Huawei phone with HMS 4.0.0.300 or later.

3) Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

Things Need To Be Done

1) Create a project in android studio.

2) Get the SHA Key. For getting the SHA key we can refer to this article.

3) Create an app in the Huawei AppGallery connect.

4) Enable push kit setting in Manage APIs section.

5) Provide the SHA Key in App Information Section.

6) Provide storage location.

7) In My Projects tab, navigate to Growing > Push and select service status Enable.

8) After completing all the above points we need to download the agconnect-services.json from App Information Section. Copy and paste the Json file in the app folder of the android project.

9) Enter the below maven url inside the repositories of buildscript and allprojects (project build.gradle file):

maven { url 'http://developer.huawei.com/repo/' }

10) Enter the below plugin in the app build.gradle file:

apply plugin: 'com.huawei.agconnect'

11) Enter the below HMS Push kit dependencies in the dependencies section:

implementation 'com.huawei.hms:push:4.0.2.300'

12) Now sync gradle

Topic-based Message Sending

The topic messaging function provided by HUAWEI Push Kit, it allows you to send messages to multiple devices whose users have subscribed to a specific topic. We 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 likes to travel can subscribe to travel information such as travel routes and travel guides.

Note the following items about topic messaging:

1) Topic messaging is best suited for delivering weather or other information that can be obtained publicly.

2) Topic messaging does not limit the number of subscriptions for each topic. However, HMS Push Kit has the following restrictions:

a) An app instance can subscribe to a maximum of 2,000 topics.

b) 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, we need to complete the following process:

1) User needs to subscribe to a topic via app.

2) After subscribing to a topic, we can send messages to the user based on the topic he/she subscribed.

Subscribed/Unsubscribed to a topic

An app can subscribed/unsubscribed to any existing topics. When an app subscribes to a topic that does not exist, the system creates a topic with the name in HMS Push Kit server. Here we will use HmsMessaging class to subscribe/unsubscribe to a topic.

We will use the subscribe method in the HmsMessaging class to subscribe to a topic. We can add a listener to listen the task execution result to check whether the subscription task is successful. Below is the code to subscribe a topic:

public void subscribe(String topic) {
try {
HmsMessaging.getInstance(getActivity()).subscribe(topic)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Task task) {
if (task.isSuccessful()) {
Log.i("TAG", "subscribe Complete");
Toast.makeText(getActivity(),"subscribe Complete",Toast.LENGTH_LONG).show();
} else {
Log.e("TAG", "subscribe failed: ret=" + task.getException().getMessage());
Toast.makeText(getActivity(),"subscribe failed: ret=" + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.e("TAG", "subscribe failed: exception=" + e.getMessage());
Toast.makeText(getActivity(),"subscribe failed: ret=" + e.getMessage(),Toast.LENGTH_LONG).show();

}
}

We will use the unsubscribe method in the HmsMessaging class to unsubscribe a topic.

public void unsubscribe(String topic) {
try {
HmsMessaging.getInstance(getActivity()).unsubscribe(topic)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Task task) {
if (task.isSuccessful()) {
Log.i("TAG", "unsubscribe Complete");
Toast.makeText(getActivity(),"unsubscribe Complete",Toast.LENGTH_LONG).show();
} else {
Log.e("TAG", "unsubscribe failed: ret=" + task.getException().getMessage());
Toast.makeText(getActivity(),"unsubscribe failed: ret=" + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.e("TAG", "unsubscribe failed: exception=" + e.getMessage());
Toast.makeText(getActivity(),"unsubscribe failed: exception=" + e.getMessage(),Toast.LENGTH_LONG).show();

}
}

Sending notification based on the topic using console

The beauty of topic based subscription is we don’t need to add any push token to send notification. We only need to provide the topic in order to send notification and the notification will be served to the devices which subscribed to that particular topic.

Note: In order to make it work, we need to subscribe to a topic in the app as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812133736.16132828162531980893171260399681:50510825011752:2800:9D744E82C6CC9AC756AF4F40454414025C99556287A98D6345C0F98E2FA2C9E6.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200812133736.16132828162531980893171260399681:50510825011752:2800:9D744E82C6CC9AC756AF4F40454414025C99556287A98D6345C0F98E2FA2C9E6.jpg[/IMG]

Here we subscribed to sports as topic. Let us see how we can send notification using topic-based subscription.

Steps required in order to send notification based on topic using AGC console:

1) In My Projects tab, navigate Growing > Push Kit and select service status Enable.

2) After enabling the service, click Add Notification button.

3) Under content provide mandatory details like Name, Type, Display, Header, Title and Body. Select Open app in Action and Homepage in App page drop-downs, as shown below:

This is not all. For full content, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202326236067510002&fid=0101187876626530001