[Huawei Developers] lokeshsuryan:
View attachment 5320357

Analytics

Analytics is a technique which is widely used in businesses today. It is essential for almost all the organizations to track the progress, user interactions and plan for the measures.

There are plenty of services available in the market which provides the access to assess the data and provide analysis report. I came across such service and tried to work on it. The results of the service are quite stable and quick.

I am talking about Huawei Analytics Kit.

Huawei Analytics kit offers data focused on intelligent decision making.

Huawei Analytics kit provides fast integration and convenient data collection.

Advantages

Simple and quick integration

Secure data-related services

Fast and convenient data collection

Real-time, comprehensive and intelligent analysis

Comprehensive user profiles

Assistance in effectively reaching users.

Push Service

Push services are widely used message sending/broadcasting service.

Which has many different ways and events to apply for different requirements.

Huawei Push kit offers different kind of message sending options as

Push Notification

Data Messages

We will be focusing on the Data Message in this article.

Data Messages

Data messages are data payloads which are handled at the device.

You can customize them in the form of key-value pair.

Use Case

This article will focus on the integration of Analytics and Push kit in the Task Scheduler application and will be showcasing how this can be helpful for the user to improve their businesses.

View attachment 5320361

Development Overview

Prerequisite

1. Must have a Huawei Developer Account

2. Must have Android Studio 3.0 or later

3. Must have a Huawei phone with HMS Core 5.0.2.300 or later

4. EMUI 9.1.0 or later

Software Requirements

1. Java SDK 1.7 or later

2. Android 5.0 or later

Preparation

1. Create an app or project in the Huawei App Gallery Connect.

2. Provide the SHA Key and App Package name of the project in App Information Section and enable the Analytics and Push Kit API.

3. Download the agconnect-services.json file.

4. Create an Android project.

Integration

1. Add below to build.gradle (project) file, under buildscript/repositories and allprojects/repositories.

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

2. Add below to build.gradle (app) file, under dependencies to use the Analytics and Push kit SDK.

dependencies{

// Import the Push SDK.
implementation 'com.huawei.hms:push:5.1.1.301'

Import the Analytics, please add Analytics SDK
implementation 'com.huawei.hms:hianalytics:5.2.0.300'

}

Tip: Minimum android version supported for these kits is 19.

3. Add below permissions to manifest file.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE"/>

Development Process

Push Kit

Adding below for receiving the Data Messages:

Below needs to be added to AndroidManifest file under <Application>:

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

Obtaining Token

Push token is important to identify the application on a device and works a unique identifier.

Client calls the getToken method in HmsInstanceId to obtain the token from the push kit server which is further used by the server to send the Push notifications to the application.

I have created a method to my Main Activity for obtaining the token as below.

private void getToken() {
// Create a thread.
new Thread() {
@Override
public void run() {
try {
// Obtain the app ID from the agconnect-service.json file.
String appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id");

// Set tokenScope to HCM.
String tokenScope = "HCM";
String token = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, tokenScope);
Log.i(TAG, "get token: " + token);

// Check whether the token is empty.
if(!TextUtils.isEmpty(token)) {
sendRegTokenToServer(token);
}
} catch (ApiException e) {
Log.e(TAG, "get token failed, " + e);
}
}
}.start();
}
// For logs
private void sendRegTokenToServer(String token) {
Log.i(TAG, "sending token to server. token:" + token);
}

Receiving Data Messages

To receive the data messages, we need to create a service and override the “onMessageReceived” method as below

import android.util.Log;
import com.huawei.hms.push.RemoteMessage;
import com.huawei.hms.push.HmsMessageService;

public class DemoHmsMessageService extends HmsMessageService {
private static final String TAG = null;

@Override
public void onMessageReceived(RemoteMessage message) {
Log.i(TAG, "onMessageReceived is called");

// Check whether the message is empty.
if (message == null) {
Log.e(TAG, "Received message entity is null!");
return;
}

// Obtain the message content.
Log.i(TAG, "get Data: " + message.getData()
+ "\n getFrom: " + message.getFrom()
+ "\n getTo: " + message.getTo()
+ "\n getMessageId: " + message.getMessageId()
+ "\n getSendTime: " + message.getSentTime()
+ "\n getDataMap: " + message.getDataOfMap()
+ "\n getMessageType: " + message.getMessageType()
+ "\n getTtl: " + message.getTtl()
+ "\n getToken: " + message.getToken());

Boolean judgeWhetherIn10s = false;
// If the message is not processed within 10 seconds, create a job to process it.
if (judgeWhetherIn10s) {
startWorkManagerJob(message);
} else {
// Process the message within 10 seconds.
processWithin10s(message);
}
}

private void startWorkManagerJob(RemoteMessage message) {
Log.d(TAG, "Start new job processing.");
}
private void processWithin10s(RemoteMessage message) {
Log.d(TAG, "Processing now.");
}}

Let’s send a Data Message to our App Users

In order to send the push notifications to the app users, we need to login to AGC.

Step 1: Choose your project

View attachment 5320375

Step 2:

Goto > Grow > Push Kit

Note: Select data processing location if see the prompt to add one.

View attachment 5320379

Step 3:

Goto > Notifications > Add Notifications

View attachment 5320381

Step 4:

Create a new notification which needs to send to user and fill the below information.

View attachment 5320383

Step 5:

Select Data message and fill the Name and Key-Value pair information.

View attachment 5320385

Step 6:

Click on Test Effect and enter the token.

Click OK.

View attachment 5320387

Step 7:

Scroll down and fill information for Push Scope as Specified device.

Fill Token for the device (Same as above).

View attachment 5320389

Step 8:

Scroll down and fill information for Push time and other parameters.

Click Submit.

View attachment 5320391

Step 9:

Click OK.

View attachment 5320395

Analytics Kit

Initialization

We will be creating instance for the analytics to get the data on the console.

private Analystics(Context context){
this.context=context;
HiAnalyticsTools.enableLog();
instance = HiAnalytics.getInstance(context);
}

public static Analystics getInstance(Context context){
if (analystics==null){
analystics=new Analystics(context);
}
return analystics;
}

Checking the Analytical data on AGC

Login to AGC

Step 1: Choose your project

View attachment 5320397

Step 2:

Goto Huawei Analytics

View attachment 5320399

Results

Goto >> Behaviour analysis >> Page analysis

View attachment 5320401

View attachment 5320403

Tips and Tricks

1. Events and attributes can be customized in Analytics kit.

2. Huawei Push Kit can work well with your own push server. So to create one, refer this.

3. Always integrate the latest version of the SDK.

4. Reports can be exported from AGC.

Conclusion

This article focuses on explaining the usage of Huawei Push kit-data message, with the help of Task Scheduler application and also showcase the analytical data to analyze the user behavior on the application which will further help to improve the user interaction.

References

GitHub

Push Kit

Analytics Kit

Orignal Source