Samachar a news application using HMS Push Kit Client Side + Server Side (Part2)

Sanghati Mukherjee
Huawei Developers
Published in
5 min readAug 28, 2020

This article is the continuity of my previous article. To get a better picture or knowledge, I would recommend you to visit my previous article first. Below is the link:

Why having your own server?

Using our own server to communicate with our app is generally the best option for sending push notification directly to our application. Because, our app will recognize and trust our own server which will allow us to control all transactions between our server and user devices.

Also if we need to send notification using Huawei AGC console, we need to copy and paste our users push token every time, which is a tedious job. Having our own server will allow us to store our users token and can send notification easily by fetching the entire list of token from database. That is what we are going to learn today.

Setting up the server

If you are not familiar with setting up your own local server and connecting the server with the device, I would strongly recommend you to go through my previous article i.e. “Build your own server from scratch to send push notification“. My previous article will help you setting up local server from scratch also it is healthy to learn something new every day as our brain can store more information than a computer.

Prerequisite

  1. We must have latest version of Node installed.
  2. We must have latest version of Visual Studio Code installed.
  3. We must have latest version of MongoDB database installed.
  4. Laptop/desktop and Huawei mobile device must share same Wi-Fi connection.
  5. We must have a working app integrate with HMS Push Kit.

We will divide this article into two parts

  1. Server Side: The server side contains Node, Express, Request and JavaScript.
  2. Client Side: The client side contains Android Native, Java, Retrofit and HMS Push Kit.

Demo

Send notification from server
Notification on device side

Goal

Our goal here is to subscribe, unsubscribe and send notification using topic-based message sending also when user will tap the notification it will take user to a specific page. All this will be done using our local server side.

Server Side

Obtaining app-level access token API

The request header uses AccessToken for authentication, which is obtained using the service API provided by the open platform. The service API provides two modes to obtain AccessToken: authorization code mode and client password mode. This API uses the client password mode.

Do not apply for a new app-level access token each time before the server API is called. Frequent application requests may trigger rejection, leading to application failure within a specified period. Each access token has a validity period.

Within the validity period, it can be used repeatedly. You are advised to apply for an access token again only when the server API is accessed and HTTP result code 401 is returned.

Subscribe to a topic API

The API is used to subscribe to a topic for one or more users. A maximum of 1,000 tokens are allowed in each call request. Currently, this API only supports Android apps.

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

API

Unsubscribe to a topic API

The API is used to unsubscribe to a topic for one or more users. A maximum of 1,000 tokens are allowed in each call request. Currently, this API only supports Android apps.

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

API

Querying the topic subscription list API

The API is used to query the topic subscription list of a token. Currently, this API only supports Android apps.

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

API

Sending notification using topic subscription API

The API is used to send notification using topic subscription also an intent to send user to specific page. In this case we are sending URL as a data in an intent which will make user to view the website in android WebActivity page on taping the notification.

URL: https://push-api.cloud.huawei.com/v1/[appid]/messages:send

API

Client Side (Android Native)

We need Retrofit in order to call our restful Apis. Include the following dependencies in app build.gradle file.

implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

Note: Here our main focus on client side is to allow user to subscribe/unsubscribe to topic also to get a list of topic subscription using push token to show user the list of topic he/she already subscribed.

Create retrofit interface

Create a new Interface class and name it Apis. Open the class, copy and paste below code:

@POST("/addToken")
Call<String> sendToken(@Body HashMap<String,String> map);

@POST("/subscribeToTopic")
Call<String> subscribeToTopic(@Body HashMap<String,String> map);

@POST("/unsubscribeToTopic")
Call<String> unSubscribeToTopic(@Body HashMap<String,String> map);

@POST("/getTopicList")
Call<JsonElement> getTopicList();

Create retrofit instance

Create a new class and name it ApiClient. Open the class, copy and paste below code:

Note: The BASE_URL is important here. We will put our IPv4 Address of our server machine instead localhost. To find our machine IPv4 Address, we will go to command prompt and type ipconfig. Also make sure that the device is connected to the same Wi-Fi the server machine is connected too.

Create service respository

Create a new class and name it NewsRepository. Open the class, copy and paste below code:

Use retrofit to call server APIs

Create an object of repository in activity as show below:

private NewsRepository mNewsRepository = new NewsRepository(Utils.SERVER_URL);

After that use this repository object to call the API of the server.

Subscribe to a topic using subscribeToTopic API

Create a method and name it subscribeToTopic(). Copy and paste the below code:

Unsubscribe to a topic using subscribeToTopic API

Create a method and name it unSubscribeToTopic(). Copy and paste the below code:

Get topic list from token API

For this we need to have user push token saved in our server database so, that we can us this token to get the list of the topic user already subscribed.

What we learn?

We learn how to subscribe/unsubscribe and get the topic subscription list using HMS Push Kit server APIs. Also, how to use these APIs on client side instead of using client code of topic-based subscription/un-subscription of HMS Push Kit.

GitHub

Very soon I am going to post the entire code on GitHub. So come back again to check it out.

--

--