[Huawei Developers] Freemind R:
[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200331053836.95450256227753610307272667437014:50510426063424:2800:54C68FA959B5D48552A1FFB36DA041F2DCFB0C9AB00C651F606ED12182F4BC93.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200331053836.95450256227753610307272667437014:50510426063424:2800:54C68FA959B5D48552A1FFB36DA041F2DCFB0C9AB00C651F606ED12182F4BC93.png[/IMG]

Huawei Push Kit is a messaging service provided by Huawei for developers. It establishes a communication channel between the cloud and devices. By using Huawei 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. Huawei 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.

Today in this article we are going to see how to integrate HMS core Push kit into your apps.

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.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327115346.81577379738155863021217111274100:50510426063424:2800:5DD7CC0510425235CC9B48421331A01430DF0EA8D35DBF13C39BB5308427A305.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327115346.81577379738155863021217111274100:50510426063424:2800:5DD7CC0510425235CC9B48421331A01430DF0EA8D35DBF13C39BB5308427A305.png[/IMG]

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 Develop 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) Now Sync the gradle.

Let’s Code

Service Class

First we need to configure app AndroidManifest.xml file. Before we go ahead and providing configuration to the AndroidManifest.xml file, we need to create a Push service class which will extend HmsMessageService class.

This service class will provide us with two callback methods i.e. onNewToken() and onMessageReceived(). In onNewToken() method we will receive the token here and onMessageReceived() method we will receive the data messages sent by Huawei Push.

public class MyPushService extends HmsMessageService {
private static final String TAG = "PushDemoLog";
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.i(TAG, "receive token:" + token);
}

@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
Log.i(TAG, "getCollapseKey: " + message.getCollapseKey()
+ "\n getData: " + message.getData()
+ "\n getFrom: " + message.getFrom()
+ "\n getTo: " + message.getTo()
+ "\n getMessageId: " + message.getMessageId()
+ "\n getSendTime: " + message.getSentTime()
+ "\n getMessageType: " + message.getMessageType()
+ "\n getTtl: " + message.getTtl());

RemoteMessage.Notification notification = message.getNotification();
if (notification != null) {
Log.i(TAG, "\n getImageUrl: " + notification.getImageUrl()
+ "\n getTitle: " + notification.getTitle()
+ "\n getTitleLocalizationKey: " + notification.getTitleLocalizationKey()
+ "\n getTitleLocalizationArgs: " + Arrays.toString(notification.getTitleLocalizationArgs())
+ "\n getBody: " + notification.getBody()
+ "\n getBodyLocalizationKey: " + notification.getBodyLocalizationKey()
+ "\n getBodyLocalizationArgs: " + Arrays.toString(notification.getBodyLocalizationArgs())
+ "\n getIcon: " + notification.getIcon()
+ "\n getSound: " + notification.getSound()
+ "\n getTag: " + notification.getTag()
+ "\n getColor: " + notification.getColor()
+ "\n getClickAction: " + notification.getClickAction()
+ "\n getChannelId: " + notification.getChannelId()
+ "\n getLink: " + notification.getLink()
+ "\n getNotifyId: " + notification.getNotifyId());
}
}
}

This service need to be mention or configure in the AndroidManifest.xml file. Copy and paste the below code after </activity> tag and before </application> tag.

<service
android:name=".MyPushService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>

To get the token from the HMS Push kit we need to write the following code in the Activity class (it could be any activity class example MainActivity).

private void getToken(){
new Thread() {
@Override
public void run() {
try {
String appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id");
pushtoken = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, "HCM");

if(!TextUtils.isEmpty(pushtoken)) {
Log.i(TAG, "get token:" + pushtoken);
showLog(pushtoken);
}
} catch (Exception e) {
Log.i(TAG,"getToken failed, " + e);

}
}
}.start();
}

On the log we will be able to see the token as shown below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327115943.69881728771388494848553798141063:50510426014451:2800:9AB04004738BFDA0B16C146DFECF27358542D916CDF5B3021067C24D90709D71.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327115943.69881728771388494848553798141063:50510426014451:2800:9AB04004738BFDA0B16C146DFECF27358542D916CDF5B3021067C24D90709D71.png[/IMG]

Turn Off / On Notification

Suppose user doesn’t want any notification from your app, we can achieve this functionality by simply calling HmsMessaging class. This class contain two methods turnOnPush() and turnOffPush(). Below you will find the code to turn off / on notification. By default the notification message is enable.

private void turnOnNotification(){
HmsMessaging.getInstance(this).turnOnPush().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
Log.i(TAG, "turnOnPush Complete");
} else {
Log.e(TAG, "turnOnPush failed: ret=" + task.getException().getMessage());
}
}
});
}

private void turnOffNotification(){
HmsMessaging.getInstance(this).turnOffPush().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
Log.i(TAG, "turnOnPush Complete");
} else {
Log.e(TAG, "turnOnPush failed: ret=" + task.getException().getMessage());
}
}
});
}

Sending Notification

To send notification message we need to go to AGC and select My apps. Under My apps we will find the app which we have created to get notification. Select the app go to Operate > Promotion > Push. Select Add Notification button. Provide details as shown below and click Test effect button which will ask for token. Put the token and select okay.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327120124.71066069728831523835479035611496:50510426063424:2800:A39680AF423468A1CEE439BD6CE8409920819C80EA3B0DB8367EB9C7FA25B671.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327120124.71066069728831523835479035611496:50510426063424:2800:A39680AF423468A1CEE439BD6CE8409920819C80EA3B0DB8367EB9C7FA25B671.png[/IMG]

We have a second part of this article i.e. HMS PUSH KIT SERVER SIDE ( PART 2 ). I would recommend you to go for it. It will help you to have a clear picture of HMS Push Kit.

That’s it

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327120248.29980497902611627036175779320210:50510426063424:2800:1148C9D31FE57F184C52461E101B21567C46BF65681B8AA8BE58413B1E59659A.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200327120248.29980497902611627036175779320210:50510426063424:2800:1148C9D31FE57F184C52461E101B21567C46BF65681B8AA8BE58413B1E59659A.jpg[/IMG]

For more information like this, you can visit https://forums.developer.huawei.com/forumPortal/en/home