[Huawei Developers] Freemind R:
Introduction

Flight booking app allows user to search and book flight. In this article, we will integrate app messaging and analytics into demo flight booking application.

For prerequisite, permission and set up, refer to part 1.

Usecase

1. We will integrate Huawei Crash analytics to monitors and captures your crashes, also it analyzes them, and then groups into manageable issues. And it does this through lightweight SDK that won’t bloat your app. You can integrate Huawei crash analytics SDK with a single line of code before you publish.

2. We will send push notification which establishes communication between cloud and devices. Using HMS push kit, developers can send message to user.

Crash Analytics

1. In order to use Crash service, we need to integrate Analytics kit by adding following code in app-level build.gradle.

implementation 'com.huawei.hms:hianalytics:5.0.1.301'

2. Enable Analytics in ACG. Refer Service Enabling.

3. Integrate Crash SDK by adding following code in app-level build.gradle.

implementation 'com.huawei.agconnect:agconnect-crash:1.4.1.300'

4. In onCreate() of MainActivity class, enable crash analytics.

AGConnectCrash.getInstance().enableCrashCollection(true);

5. To trigger a crash

AGConnectCrash.getInstance().testIt(this);

Note: Please comment the above code before releasing or publishing the app. It deliberately causes app to crash and just used for testing HMS crash service.

6. To monitor a crash report inApp Gallery Connect, select your app from MyProjects. Select Quality -> Crash on left panel of the screen. Click on Problems.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918070833.88261003202870623784912879852562:50510928022349:2800:B7C2AD69D2785D4D792C4B47DD7976376D916720D663A99CB4646702920D8756.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918070833.88261003202870623784912879852562:50510928022349:2800:B7C2AD69D2785D4D792C4B47DD7976376D916720D663A99CB4646702920D8756.png[/IMG]

7. Setting screen has been designed to enable crash service.

Push Kit

1. Enable push kit in AGC. Refer Service Enabling.

2. Integrate Push kit SDK by adding following code in app-level build.gradle.

implementation 'com.huawei.hms:push:5.0.1.300'

3. Call getToken() method in onCreate() of MainActivity class. Generated token will be used later in AGC to send notification.

private void getToken() {
new Thread() {
@Override
public void run() {
try {
// read from agconnect-services.json
String appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id");
String token = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, "HCM");
Log.i(TAG, "get token:" + token);
if(!TextUtils.isEmpty(token)) {
sendRegTokenToServer(token);
}
} catch (ApiException e) {
Log.e(TAG, "get token failed, " + e);
}
}
}.start();
}
private void sendRegTokenToServer(String token) {
Log.i(TAG, "sending token to server. token:" + token);
}

4. To deregister the token.

new Thread() {
@Override
public void run() {
try {
// read from agconnect-services.json
String appId = AGConnectServicesConfig.fromContext(this).getString("client/app_id");
HmsInstanceId.getInstance(this).deleteToken(appId, "HCM");
Log.i(TAG, "deleteToken success.");
} catch (ApiException e) {
Log.e(TAG, "deleteToken failed." + e);
}
}
}.start();

5. Create a Service which extends HmsMessageService class. Declare it in Manifest.

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

6. Override onMessageReceivedMethod() to obtain the message data.

@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());
}
}

7. Log in to AGC, select your project and navigate to Growing -> Push kit. And compose the message for notification. Provide the token which was earlier generated from getToken() method.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918072008.99964230508019133905622649002304:50510928022349:2800:368DA631A1245FE40FE4A8111A990A5AE000DD9F90A78533A0E22F9AD2075AE0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918072008.99964230508019133905622649002304:50510928022349:2800:368DA631A1245FE40FE4A8111A990A5AE000DD9F90A78533A0E22F9AD2075AE0.png[/IMG]

Image

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918072056.24324688101605506440266177587334:50510928022349:2800:E7829B3AAF0F9DF542C95D8E8FF3B23E368AD9F65831749843DFC521B128DBA0.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200918072056.24324688101605506440266177587334:50510928022349:2800:E7829B3AAF0F9DF542C95D8E8FF3B23E368AD9F65831749843DFC521B128DBA0.jpg[/IMG]

Conclusion:

In this article, we learnt how to integrate Huawei crash service and HMS push kit to in application.

For detailed guide, refer to

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-crash-introduction