Integrating Huawei Core Push Kit
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
- Must have a Huawei Developer Account
- Must have a Huawei phone with HMS 4.0.0.300 or later
- 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
- First we need to create a project in android studio.
- Get the SHA Key. For getting the SHA key we can refer to this article.
- Create an app in the Huawei app gallery connect.
- Enable push kit setting in Manage APIs section.
- Provide the SHA Key in App Information Section.
- Provide storage location.
- Under Develop tab, go to Growing > Push and select service status Enable.
- 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.
- 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’
Also implementation ‘com.huawei.hms:push:4.0.2.300’ in the dependencies section.
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>
Get the token
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);
}
} catch (Exception e) {
Log.i(TAG,"getToken failed, " + e);
}
}
}.start();
}
On the log we will be able to see the token as shown below
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.
That’s it
For More Information
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
HAPPY CODING…