[Huawei Developers] Freemind R:
This article is originally from HUAWEI Developer Forum

Forum link: https://forums.developer.huawei.com/forumPortal/en/home

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512080246.34247756787195219553158030863404:50510525095247:2800:778730D37626A92D1E37CF9AB0047704A19E69F914D8BFA84948C174A6048079.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512080246.34247756787195219553158030863404:50510525095247:2800:778730D37626A92D1E37CF9AB0047704A19E69F914D8BFA84948C174A6048079.png[/IMG]​

HMS In-App Messaging is a tool to send relevant messages to target users actively using our app to encourage them to use key app functions. For example, we can send in-app messages to encourage users to subscribe to certain products, provide tips on passing a game level, or recommend activities of a restaurant.

In-App Messaging also allow us to customize our messages, the way it will be sent and also define events for triggering message sending to our users at the right moment.

Today I am going to server you a recipe to integrate In-App Messaging in your apps within 10 minutes.

Key Ingredients Needed

· 1 Android App Project, which supports Android 4.2 and later version.

· 1 SHA Key

· 1 Huawei Developer Account

· 1 Huawei phone with HMS 4.0.0.300 or later

Preparation Needed

· First we need to create an app or project in the Huawei app gallery connect.

· Provide the SHA Key and App Package name of the android project in App Information Section.

· Provide storage location in convention section under project setting.

· Enable App Messaging setting in Manage APIs section.

· 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 respectively (project build.gradle file)

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

· Copy and paste the below class path inside the dependency section of project build.gradle file.

classpath 'com.huawei.agconnect:agcp:1.3.1.300'

· Copy and paste the below plugin in the app build.gradle file

apply plugin: 'com.huawei.agconnect'

· Copy and paste below library in the app build.gradle file dependencies section

implementation 'com.huawei.agconnect:agconnect-appmessaging:1.3.1.300'

· Put the below permission in AndroidManifest file.

a) android.permission.INTERNET

b) android.permission.ACCESS_NETWORK_STATE

· Now Sync the gradle.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512211608.24592209383015254576304713312729:50510525095247:2800:5412D6ADABB280AE7E60E25E4226C255C618F960F8AD3EC3664437E3A147BFF6.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512211608.24592209383015254576304713312729:50510525095247:2800:5412D6ADABB280AE7E60E25E4226C255C618F960F8AD3EC3664437E3A147BFF6.gif[/IMG]​

Android Code

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

2. 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);
}
});

3. To initialize the AGConnectAppMessaging instance we will use.

AGConnectAppMessaging appMessaging = AGConnectAppMessaging.getInstance();

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

appMessaging.setFetchMessageEnable(true);

5. To enable message display.

appMessaging.setDisplayEnable(true);

6. 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.

Till now we added libraries, wrote the code in android studio using java etc. in the heated pan and the result will look like this:

public class MainActivity extends AppCompatActivity {
private String TAG = "MainActivity";
private AGConnectAppMessaging appMessaging;
private HiAnalyticsInstance instance;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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);
}
});
HiAnalyticsTools.enableLog();
instance = HiAnalytics.getInstance(this);
instance.setAnalyticsEnabled(true);
instance.regHmsSvcEvent();
inAppMessaging();
}

private void inAppMessaging() {
appMessaging = AGConnectAppMessaging.getInstance();
appMessaging.setFetchMessageEnable(true);
appMessaging.setDisplayEnable(true);
appMessaging.setForceFetch();
}
}

App Gallery Connect

Before we start using one of our key ingredients i.e. Huawei App Gallery Connect using Huawei Developer Account to serve In-App Messages, I would like to inform you that we can server the main dish into three different flavour or types:

A. Pop-up Message Flavour

B. Banner Message Flavour

C. An Image Message Flavour

In-App Message Using Pop-up Flavour

Let’s serve the main dish using Pop-up Message Flavour.

Step 1: Go to Huawei App Gallery Connect

Step 2: Select My projects.

Step 3: After selecting my projects, select the project which we have create earlier. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212453.93759597053021556523249955296977:50510525095247:2800:6F1AA69413D46BA4640B4D6EE392B1DC79C34E6ED200D60F1D8328AEF6B93EB3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212453.93759597053021556523249955296977:50510525095247:2800:6F1AA69413D46BA4640B4D6EE392B1DC79C34E6ED200D60F1D8328AEF6B93EB3.png[/IMG]​

Step 4: After selecting the project, we will select App Messaging from the menu. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212516.46140922281273117073682893805476:50510525095247:2800:263D0ADF21B50DEC6CFA7F06FF78AA28504B31A6B47974D9CA947BF9D663C31E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212516.46140922281273117073682893805476:50510525095247:2800:263D0ADF21B50DEC6CFA7F06FF78AA28504B31A6B47974D9CA947BF9D663C31E.png[/IMG]​

Step 5: Select New button to create new In-App Message to send to the device.

Step 6: Provide the Name and Description. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212547.22724868448403402277104901346952:50510525101557:2800:C6EFDAA2A24DABA0D28B5E355FEC5483C45EAAD09462F876011C80304876E4B9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212547.22724868448403402277104901346952:50510525101557:2800:C6EFDAA2A24DABA0D28B5E355FEC5483C45EAAD09462F876011C80304876E4B9.png[/IMG]​

Step 7: In the Set style and content section, we have to select the type (which is Pop-up), provide a title, a message body, and choose colour for title text, body text and the background of the message. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212615.07478508239030702067132052200434:50510525095247:2800:B73BBA3EACE32418E176996B77FE411F0516A42D7FE69DEE4362A3924B4C477D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212615.07478508239030702067132052200434:50510525095247:2800:B73BBA3EACE32418E176996B77FE411F0516A42D7FE69DEE4362A3924B4C477D.png[/IMG]​

Step 8: After providing the details in set style and content section, we will move on to the Image section. We will provide two image here for portrait and landscape mode of the app. Remember for portrait the image aspect ratio should be 3:2 (300x200) and for landscape the image aspect ratio should be either 1:1 (100x100) or 3:2 (300x200). It will look like this:

https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212645.95630192255042904397797509198276:50510525101557:2800:FA1CFD7E5EA43100725399BB4787A4B9FB44B8E02776FF83D09816D732E0AA63.png​

Step 9: We can also provide a button in the Pop-up message using Button Section. The button contains an action. This Action contains two option. We can provide user with Disable Message action or redirect user to particular URL. The section will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212749.68088790150847358667386989836555:50510525101557:2800:A5401B7B69D0EF29E51154CE21C639DE6D94BE5135FA4D2E32CF87B456968ED9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212749.68088790150847358667386989836555:50510525101557:2800:A5401B7B69D0EF29E51154CE21C639DE6D94BE5135FA4D2E32CF87B456968ED9.png[/IMG]​

Step 10: We will now move to the next section i.e Select Sending Target section. Here we can click New condition to add a condition for matching target users. Condition types include app version, OS version, language, country/region, audience, user attributes, last interaction, and initial startup.

Note: In this article, we didn’t used any condition. But you are free to use any condition to send targeted In-App Messages.

The section will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212827.70914627722393224466803070177876:50510525095247:2800:1DD4A8EE3C1B06517AD12916C8B5A6356A8288C5846D7823942D16FA7B87F3E3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212827.70914627722393224466803070177876:50510525095247:2800:1DD4A8EE3C1B06517AD12916C8B5A6356A8288C5846D7823942D16FA7B87F3E3.png[/IMG]​

Step 11: The next section is the Set Sending Time section.

a) We can schedule a date and time to send message.

b) We can also provide an End date and time to stop sending message.

c) We can also display message on an events by using trigger event functionality. For example, we can display a discount of an item in a shopping app. A trigger event is required for each in-app message.

d) Also we can flexibly set the frequency for displaying the message.

This is how it will look:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212858.51589201670391663931790762165984:50510525095247:2800:4A4386E49938A627A097716B93780A09AA52A59776DA2947D9DCF6856885B675.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212858.51589201670391663931790762165984:50510525095247:2800:4A4386E49938A627A097716B93780A09AA52A59776DA2947D9DCF6856885B675.png[/IMG]​

Step 12: The last section is the Set Conversion Event section. As off now we will keep it as none. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212915.86955783558406179782471662723710:50510525095247:2800:08716AFA17A57741CC7F4FB911C2557C64E3D245CBB20C9E747087C9894DF1E9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212915.86955783558406179782471662723710:50510525095247:2800:08716AFA17A57741CC7F4FB911C2557C64E3D245CBB20C9E747087C9894DF1E9.png[/IMG]​

Step 13: Click Save in the upper right corner to complete message creation. Also we can click Preview to preview the display effect of your message on a mobile phone or tablet.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212948.49929462708140802304680771191019:50510525095247:2800:24189CB8E3F843CF2751392D9FB670D62261D93F133D5FA9912C5BC7A343BDD5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512212948.49929462708140802304680771191019:50510525095247:2800:24189CB8E3F843CF2751392D9FB670D62261D93F133D5FA9912C5BC7A343BDD5.png[/IMG]​

Note: Do not hit the publish button yet. Just save it.

Step 14: 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. To that we need to find the message that you need to test, and click Test in the Operation column as it is shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213020.18349925076671693317726930735398:50510525101557:2800:A698F2668316A8E85A369A1E183FAA7A0353274861599C9C9EBBC32A1345619D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213020.18349925076671693317726930735398:50510525101557:2800:A698F2668316A8E85A369A1E183FAA7A0353274861599C9C9EBBC32A1345619D.png[/IMG]​

Step 15: Click Add test user and enter the AAID of the test device in the text box. Also run the app in order to find AAID of the test device in the logcat of the Android Studio.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213033.24407189143556205711556820346318:50510525101557:2800:160EC347D72A22BFB9AF290506597EE7288AD75B717A4295960B33FC0F7AC069.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213033.24407189143556205711556820346318:50510525101557:2800:160EC347D72A22BFB9AF290506597EE7288AD75B717A4295960B33FC0F7AC069.png[/IMG]​

Step 16: Finally we will publish the message. We will select publish in the operation column. It will look like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213100.52555074488031707697621957493697:50510525101557:2800:8612965086D654A8DEAEFE622EF2AD0682513EFCB513DFA13554CC6C2E026F79.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213100.52555074488031707697621957493697:50510525101557:2800:8612965086D654A8DEAEFE622EF2AD0682513EFCB513DFA13554CC6C2E026F79.png[/IMG]​

The Dish

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213413.88521240683879524917149553735477:50510525095247:2800:6DCE31FD75EBA6D6A5B35672CF32ED8DE7479604BBB894211F5353B418AD35CE.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213413.88521240683879524917149553735477:50510525095247:2800:6DCE31FD75EBA6D6A5B35672CF32ED8DE7479604BBB894211F5353B418AD35CE.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213205.77303556353577019861085975310957:50510525095247:2800:05F938987D1DA169CDD1EAF7ED3A9C6D33647418BA0B54C0B347B6A344069DC1.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200512213205.77303556353577019861085975310957:50510525095247:2800:05F938987D1DA169CDD1EAF7ED3A9C6D33647418BA0B54C0B347B6A344069DC1.jpg[/IMG]​