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

Introduction

In-App Messaging helps you engage your app's active users by sending targeted, contextual messages that encourage to use key app features. For example, you could send an in-app message to get users to subscribe, watch a video, complete a level, or buy an item. You can customize messages as cards, banners, modals, or images, and set up triggers so that they appear exactly when benefit your users most.

Usecase

1. Add following image on Huawei App gallery connect console and google firebase console to be shown as in-app message in our app.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806140516.96431908022331095341127329942337:50510817015426:2800:B8B6210DFA673EAABBF8F1E670CBC5A5FB89A2F48011808415717E56A62C81F9.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806140516.96431908022331095341127329942337:50510817015426:2800:B8B6210DFA673EAABBF8F1E670CBC5A5FB89A2F48011808415717E56A62C81F9.jpg[/IMG]

2. App installed on Huawei device will fetch in-app message (image) from Huawei App gallery connect.

3. App installed on GMS devices will fetch in-app message (image) from Google firebase console.

Huawei In-App Messaging

Prerequisite

1. You must have Huawei developer account.

2. Huawei phone with HMS 4.0.0.300 or later

3. Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

Setup:

1. Create a project in android studio.

2. Get the SHA-256 Key.

3. Create an app in the Huawei app gallery connect.

4. Enable account kit setting in Manage APIs section.

5. Provide the SHA-256 Key in App Information Section.

6. Provide storage location.

7. After completing all the above points we need to download the agconnect-services.json from App Information Section. Place the json file in 　 the app folder of the android project.

8. Enter the below maven url inside the repositories of buildscript and allprojects respectively (project build.gradle file )

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

9. Enter the below plugin in the app build.gradle file

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

10. Put the below permission in AndroidManifest file.

a) android.permission.INTERNET

b) android.permission.ACCESS_NETWORK_STATE

11. Now Sync the gradle.

Implemetation

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

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.20200806142356.35233489537659925596111206949399:50510817015426:2800:273E12886E761166C46AD18288BF5BF3BBB88A6C542128B568C7AAFE7DF897BC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806142356.35233489537659925596111206949399:50510817015426:2800:273E12886E761166C46AD18288BF5BF3BBB88A6C542128B568C7AAFE7DF897BC.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 date and time to stop sending message.

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.

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.20200806142754.58121868743280260872070657604955:50510817015426:2800:4FE66EDA17D75AD6C628E6C62936B4E52A0F1680D1C4CB9823E487D5C1C89362.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806142754.58121868743280260872070657604955:50510817015426:2800:4FE66EDA17D75AD6C628E6C62936B4E52A0F1680D1C4CB9823E487D5C1C89362.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.20200806143036.71288796014178258578954826984619:50510817015426:2800:377774EE4528578E894DD7C828F7820B9E547FF315B16E39A2C425379EB117A8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806143036.71288796014178258578954826984619:50510817015426:2800:377774EE4528578E894DD7C828F7820B9E547FF315B16E39A2C425379EB117A8.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.20200806143241.00733357206945169660902248534332:50510817015426:2800:0EFA8988D1C22BE4AEDD6D3558878E3583B2B06EC890DA2C8ADC8500B587E9C0.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806143241.00733357206945169660902248534332:50510817015426:2800:0EFA8988D1C22BE4AEDD6D3558878E3583B2B06EC890DA2C8ADC8500B587E9C0.jpg[/IMG]

Google firebase In-App Messaging

Steps:

1. To enable Firebase in your app, add following lines in your root-level build.gradle file.

buildscript {

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}

dependencies {
// ...

// Add the following line:
classpath 'com.google.gms:google-services:4.3.3' // Google Services plugin
}
}

allprojects {
// ...

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
// ...
}
}

2. In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin:

apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services' // Google Services plugin

android {
// ...
}

3. To your module (app-level) Gradle file (usually app/build.gradle), add the dependencies.

dependencies {

// Add the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics:17.4.4'
implementation 'com.google.firebase:firebase-auth:19.3.2'

}

4. That testing device is determined by a FirebaseInstallations ID, or FID. Find your testing app's FID by checking the Logcat in Android Studio for the following `Info` level log:

I/FIAM.Headless: Starting InAppMessaging runtime with Installation ID YOUR_INSTALLATION_ID

We will add this FID while composing in-app message on firebase console.

5. Set up your new campaign in the Firebase consoles Firebase In-App Messaging page.

6. Select your project and click Create a new campaign.

7. In this article, we will use image in-app message. Select message layout as Image only. Provide Image URL and click Next.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806144957.38849302965744267379431985545359:50510817015426:2800:492BE3B15FEADFB417F5901AEA84F327204A111E40F431AE158F3F0DCF6D9426.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806144957.38849302965744267379431985545359:50510817015426:2800:492BE3B15FEADFB417F5901AEA84F327204A111E40F431AE158F3F0DCF6D9426.png[/IMG]

8. Provide campaign name and description and define your target user. Here we have not defined any target user.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145150.45357249008529363175095239887013:50510817015426:2800:EA16372D60C72199FF8E01D9DF3805E689B9D2F58949C3B8557B6577C202DDFD.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145150.45357249008529363175095239887013:50510817015426:2800:EA16372D60C72199FF8E01D9DF3805E689B9D2F58949C3B8557B6577C202DDFD.png[/IMG]

9. Click Next to define campaign start time, end time, trigger event and frequency limit of in-app message.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145341.83839249316263564947180817323563:50510817015426:2800:F74B7103035C4840D72411C55777542184ED0BA894DC276B00003DAA50D09BE5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145341.83839249316263564947180817323563:50510817015426:2800:F74B7103035C4840D72411C55777542184ED0BA894DC276B00003DAA50D09BE5.png[/IMG]

10. Click Next and provide conditional events and additional options. These are optional. Click Save as draft.

11. Select Test on Device, and add the earlier generated FID.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145524.02772795432900244332071790866987:50510817015426:2800:E449F2BB15EDBBE45A58C67E924F422DD3792CFBC65CD8BEA66707F288EFEA14.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200806145524.02772795432900244332071790866987:50510817015426:2800:E449F2BB15EDBBE45A58C67E924F422DD3792CFBC65CD8BEA66707F288EFEA14.png[/IMG]

12. Run the application on GMS installed device, you will see Google in-app message.

To check if device has HMS or GMS installed

public class Utils {

public static boolean isSignedIn = false;

public static boolean isGooglePlayServicesAvailable(Activity activity) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
if(status != ConnectionResult.SUCCESS) {
if(googleApiAvailability.isUserResolvableError(status)) {
googleApiAvailability.getErrorDialog(activity, status, 2404).show();
}
return false;
}
return true;
}

public static boolean isHuaweiMobileServicesAvailable(Context context) {
if (HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context) == ConnectionResult.SUCCESS){
return true;
}
return false;
}

public static boolean isDeviceHuaweiManufacturer () {

String manufacturer = Build.MANUFACTURER;
Log.d("Device : " , manufacturer);
if (manufacturer.toLowerCase().contains("huawei")) {
return true;
}
return false;

}
}

References:

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

https://firebase.google.com/docs/in-app-messaging/get-started?platform=android