[Huawei Developers] John Wo:
More information like this, you can visit HUAWEI Developer Forum​

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.

UseCase

1. We will show app-messaging image on app screen showing list of searched flight. When user tap on app-messaging image, it will redirect to webpage showing guidelines for international arrivals and departures. Add following image URL on Huawei App gallery connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911083530.44069123712108429642069155606026:50510922064340:2800:6F45029818805325850ADF2AD832DB742DDFA92C10D39FBAB2BDA4E0C1517076.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911083530.44069123712108429642069155606026:50510922064340:2800:6F45029818805325850ADF2AD832DB742DDFA92C10D39FBAB2BDA4E0C1517076.jpg[/IMG]

2. We will use Huawei analytics to report few events like successful sign in and number of flights available between source destination.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911095457.61797695605478221903215503056736:50510922064340:2800:A3E5BC388E407C1E44576A6512BC03938DF7974CC34AB61A66D03B4BC7FE4C91.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911095457.61797695605478221903215503056736:50510922064340:2800:A3E5BC388E407C1E44576A6512BC03938DF7974CC34AB61A66D03B4BC7FE4C91.gif[/IMG]

For prerequisite and set up, refer to part 1.

Huawei App messaging

App Messaging of AppGallery Connect used to send relevant messages to target users actively using your app to encourage them to use key app functions. For example, you can send in-app messages to encourage users to subscribe certain products, provide tips on passing a game level, or recommend activities of a restaurant.

Implemetation

1. Add dependency in the app build.gradle file.

dependencies {
implementation 'com.huawei.agconnect:agconnect-appmessaging:1.4.0.300'
}

2. We need AAID for later use in sending In-App Messages. To obtain AAID, we will use getAAID() method.

3. Add following code in your project to obtain AAID.

HmsInstanceId inst = HmsInstanceId.getInstance(this);
Task<AAIDResult> idResult = inst.getAAID();
idResult.addOnSuccessListener(new OnSuccessListener<AAIDResult>() {
@Override
public void onSuccess(AAIDResult aaidResult) {
String aaid = aaidResult.getId();
Log.d(TAG, "getAAID success:" + aaid );
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.d(TAG, "getAAID failure:" + e);
}
});

4. To initialize the AGConnectAppMessaging instance we will use.

AGConnectAppMessaging appMessaging = AGConnectAppMessaging.getInstance();

5. To allow data synchronization from the AppGallery Connect server we will use.

appMessaging.setFetchMessageEnable(true);

6. To enable message display.

appMessaging.setDisplayEnable(true);

7. To specify that the in-app message data must be obtained from the AppGallery Connect server by force we will use.

appMessaging.setForceFetch();

Since we are using a test device to demonstrate the use of In-App Messaging, so we use setForceFetch API. The setForceFetch API can be used only for message testing. Also In-app messages can only be displayed to users who have installed our officially released app version.

Creating Image In-App Messaging on App Gallery connect

1. Sign in to AppGallery Connect and select My projects.

2. Select your project from the project list.

3. Navigate Growing > App Messaging and click New.

4. Set Name and Description for your in-app message.

5. Provide the in-app message Type. For image in-app message, select type as Image.

6. Provide image URL.

7. Provide the action for image tapping.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911084635.08047745776128120095166443067399:50510922064340:2800:4B16CC8B1891E23712C49F75F33E52DC0868BB827435D92B2A2F00993748D259.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911084635.08047745776128120095166443067399:50510922064340:2800:4B16CC8B1891E23712C49F75F33E52DC0868BB827435D92B2A2F00993748D259.png[/IMG]

8. Click Next to move on to the next section, that is, Select Sending Target section. Here we can define Condition for matching target users. In this article, we did not use any condition.

9. The next section is the Set Sending Time section.

We can schedule a date and time to send in-app message.

We can also provide an End data and time to stop sending message.

We can also display message on an events by using trigger event functionality.

Also we can flexibly set the frequency for displaying the in-app message.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085339.26927763087457449460445165817034:50510922064340:2800:467D23F6784239B6240E485D64CD36E27FE72070C385FF72EF306610C37AB6AE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085339.26927763087457449460445165817034:50510922064340:2800:467D23F6784239B6240E485D64CD36E27FE72070C385FF72EF306610C37AB6AE.png[/IMG]

10. Click Next, find Set conversation events. It associates the display or tap of the app message with a conversion event. The conversion relationship will be displayed in statistics. As off now we will keep it as none.

11. Click Save in the upper-right corner to complete message creation.

12. In-app messages can only be displayed to users who have installed our officially released app version. App Messaging allows us to test an in-app message when our app is still under tests. Find the message that you need to test, and click Test in the Operation column as shown below.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085456.58323359205489758528807076225764:50510922064340:2800:8C56F57532EE5D4151F4331D7262B55E693904931D7FB6CC189FD7BF3D40861E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085456.58323359205489758528807076225764:50510922064340:2800:8C56F57532EE5D4151F4331D7262B55E693904931D7FB6CC189FD7BF3D40861E.png[/IMG]

13. Provide the AAID of the test device in the text box. Click Save.

14. Click Publish.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085615.70191553206079545455744987280678:50510922064340:2800:D01E0237C40158B5542703964854E901AEAC8816AA4A24A9437E33B775A8E92B.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911085615.70191553206079545455744987280678:50510922064340:2800:D01E0237C40158B5542703964854E901AEAC8816AA4A24A9437E33B775A8E92B.gif[/IMG]

Huawei Analytics

In this section, will show you how to use main APIs of HUAWEI Analytics Kit to report some custom events. App will report event like successful login, number of search flights.

1. Add the dependencies in app build.gradle file.

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

2. The MainActivity.java is launched once user has successfully signed in. In onCreate(), we will call initializeAnalytics()to report successful signed in event.

private void initializeAnalytics() {
//Enable Analytics Kit Log
HiAnalyticsTools.enableLog();
//Generate the Analytics Instance
instance = HiAnalytics.getInstance(this);
//Parameter definition
Bundle bundle = new Bundle();
bundle.putBoolean("IsLoggedIn", true);

//Reporting event
instance.onEvent("LoginStatus", bundle);

}

3. To report number of flight available between origin and destination.

httpHandler.setFlightSearchListner(new OnFightSearchListner() {
@Override
public void onSuccess(ArrayList<Quote> quoteList) {

if(quoteList!= null && quoteList.size() > 0) {
Bundle bundle = new Bundle();
bundle.putBoolean("FlightSearchStatus", true);
bundle.putInt("FlightSearchCount", quoteList.size());
//Reporting event
instance.onEvent("FlightSearchStatus", bundle);
}
adapter = new OneWayFlightAdapter(quoteList);
recyclerView.setAdapter(adapter);

}

@Override
public void OnFailure(Exception e) {
// not implemented yet.. will do later
Log.e(TAG, e.getMessage());
}
});

To Check Analysis on App Gallery Connect

1. On the left side panel, click on Huawei Analyze > Behaviour Analysis > Event Analysis.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911090259.98678275396134739392806232829011:50510922064340:2800:A6A9ED57FD097A369645193ACB9D3BBF0BDE7EFDAE5536EA7E1207385DBF4AF5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200911090259.98678275396134739392806232829011:50510922064340:2800:A6A9ED57FD097A369645193ACB9D3BBF0BDE7EFDAE5536EA7E1207385DBF4AF5.png[/IMG]

Conclusion:

In this article, we learnt how to integrate Huawei app-messaging and analytics to report event in application.