Huawei Developers
Published in

Huawei Developers

Samachar a news application using HMS Push Kit Client Side (Part1)

Huawei Push Kit is a messaging service provided by Huawei for developers. It establishes a communication channel between the cloud and devices. By using HMS Push Kit, developers can send the latest messages to users. This helps developers 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 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 improve the DAU of your apps.

In my earlier articles, we learned how to send notification using AGC as well as from server. If you are new to this topic checkout my previous two article:

1) HMS Push Kit Client Side (Part1)

2) HMS Push Kit Server Side (Part2)

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

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. First we need to 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 app gallery connect.
  4. Enable push kit setting in Manage APIs section.
  5. Provide the SHA Key in App Information Section.
  6. Provide storage location.
  7. Under My Projects tab, go 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. Copy and paste the below maven url inside the repositories of buildscript and allprojects (project build.gradle file):
maven { url ‘http://developer.huawei.com/repo/' }

10. Copy and paste the below plugin in the app build.gradle file:

apply plugin: ‘com.huawei.agconnect’

11. Copy and paste the below HMS Push kit dependencies in the dependencies section:

implementation ‘com.huawei.hms:push:4.0.2.300’

12. Now Sync the gradle.

Topic-based Message Sending

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. 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 like travelling 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 subscribe/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 to the task execution result to check whether the subscription task is successful. Below is the code to subscribe to a topic:

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

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:

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. Under My Projects tab, go to Growing > Push Kit and select service status Enable.
  2. After enabling the service, click on Add Notification button.
  3. Under content provide mandatory details like Name, Type, Display, Header, Title and Body. Select Action as Open app and App page as Homepage as shown below:

4. Select Push scope as Subscriber as shown below:

5. Select Condition as Subscribed and topic as sports (whatever topic user subscribed) as shown below:

6. Click on Custom expression and it will show a dialog where we need to select the topic as sports. After that click Okay.

7. Need to set Push time to one-off time (Now and Scheduled) or periodic time (Every day and Every week). Time zone is mandatory for all push time values except Now. Duration can be modified, whose value is 1 day by default.

8. Once we complete the above steps click submit button at the top right corner and it show the details of notification as shown below:

9. Select OK and we are done. Check the result below:

Opening a specific page of an app

We can customize the actions to be triggered when users tap notification messages, such as opening the home page of an app, accessing a specific URL, opening customized rich media, or opening a specified page of an app. Here we will focus on opening a specified page of our app.

Settings required in client side:

  1. The value of the action parameter must be same as the action name set in the intent-filter section of the Activity class registered in the AndroidManifest.xml file of the client. Copy and paste the intent-filter as shown below:

Here we want user to tap the notification and go to WebActivity page to view the data we passed via intent.

2. When user select or tap the notification, it will open the WebActivity page and user will be able to view the URL which is passed using intent with the help of Webview widget of android. Copy and paste below code in WebActivity class:

Steps required in order to send notification based on topic and make user open a specific page using AGC console:

Follow the above the steps to send notification based on topic subscription. The only thing we need to change is in the content section App page of console where we will select Custom intent URI page and below we will provide intent URI as shown below:

Here we will provide intent URI as shown below:

intent://com.huawei.huaweinewsapp/weburl?#Intent;scheme=pushscheme;launchFlags=0x4000000;S.url=https://www.derwesten.de/sport/fussball/bvb/borussia-dortmund-bvb-training-vorbereitung-trainingslager-schweiz-bad-ragaz-u19-nnamdi-collins-moukoko-sancho-news-personal-id230087054.html;end

If we notice carefully com.huawei.huaweinewsapp is the host, weburl is the path and pushscheme is the scheme of intent-filter provided in Android Manifest file data section. S.url means that the variable value of url is a String (denoted as S in this case and if it is integer it will denoted as i) and the value it contains is the URL which will be fetched once user tap notification and will be able to view the website using webview widget of android in WebActivity page. Check the result below:

How to show user the topic is subscribed whenever they open the app?

As of now we don’t have any java code to check what topic user subscribed to but we do have server API which is used to query the topic subscription list of a token. Using this API we will verify whether user subscribed to any topic or not. Very soon I am going to post an article based on the server side which will cover the topic based subscription of HMS Push Kit.

FAQs

1) HMS push notification can also send notification even our application is removed from the background or stack.

2) We do not need to put a token in order to send notification based on topic subscription.

3) Currently, services provided by HUAWEI Push Kit are free of charge.

4) A maximum of 3,000 messages can be sent to an app on a device every day. If the number exceeds 3,000, messaging traffic to the app will be limited for 24 hours. If the number of messages exceeds 100,000, HUAWEI Push Kit will be directly disabled. In this case, rectification must be performed and a rectification plan must be submitted to apply for HUAWEI Push Kit again.

Server Side (Part 2)

Coming Soon…

GitHub

Coming soon…

For more reference

  1. https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-introduction
  2. https://developer.huawei.com/consumer/en/codelab/HMSPushKit/index.html#0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store