[Huawei Developers] devwithzachary:
Huawei AppGallery Connect’s App Messaging service allows you to send targeted, useful in app messages to your users.

The look and content of messages are completely customisable, and there is a wide range of triggers and filters that can be used to decide who will receive a message and when.

Lets take a look today at how we can set this up to work within an Android project.

As always we will start with a fresh project but of course you can just as easily use this guide to build the service into an app you already have!

Enabling the Service​

Sign in to AppGallery Connect, click My projects, click your project, go to Grow > App Messaging, and click Use now. For more information, please refer to App Messaging.

If you do not have an Android project, create one first.

After completing these steps, you can start creating an in-app message.

Click New in the upper right corner.

Set Name and Description

Set the style and content and click Next.

Select a message type from the Layout drop-down list, and set its style and content. Currently, the Modal, Image, and Banner message types are supported.

Set target users and click Next.

In App, select the name of the app package for which you need to publish the in-app message.

You can click New condition to add a condition for matching target users, which include app version, OS version, language, country/region, audience, and more. Among the types, User attributes are defined under HUAWEI Analytics > Management > User attributes, and Prediction is defined by creating prediction tasks under My projects > Grow > Prediction.

Set the message sending time

Message display is triggered by specific events. App Messaging supports two types of trigger events: preset events and HUAWEI Analytics events.

(Optional) Set conversion events. Before setting a conversion event, you need to toggle it on first, which can be done as follows:

Go to HUAWEI Analytics > Management > Events and toggle Mark as conversion event and Event switch on for the specified event. In addition to the events the SDK collects, you can also create a preset or custom event for event tracking and analysis.

Finally, Click Save or Publish.

Integrating the Service SDK​

Add Maven repository configuration to the build.gradle file of your project.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.huawei.agconnect:agcp:1.5.2.300'
}
}

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

Open the app-level build.gradle file and configure the App Messaging SDK

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
android {…..}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'com.huawei.agconnect:agconnect-appmessaging:1.5.2.300'
}

In AppGallery Connect, click My projects, and find your project from the list. Go to Project settings > General information, download the agconnect-services.json file, and save the file to the app directory of your Android project.

Displaying an In-App Message​

If you choose to display a message using the default message layout, the development process is totally coding-free.

By integrating the SDK as above you are all good to go, your creating message will be displayed as per its filters and triggers.

You can also call APIs provided by the service SDK to customize your in-app message.

Customising the Message Layout​

First, you need to customize a message display class. To do so, implement AGConnectAppMessagingDisplay and override the displayMessage method.

public class CustomDisplayView implements AGConnectAppMessagingDisplay {
@Override
public void displayMessage(@NonNull AppMessage appMessage,
@NonNull AGConnectAppMessagingCallback callback) {

}
}

Define an AlertDialog in displayMessage and copy the layout file to the code. The sample code is as follows:

@Override
public void displayMessage(@NonNull AppMessage appMessage, @NonNull AGConnectAppMessagingCallback callback) {
Log.d(TAG, appMessage.getId() + "");
showDialog(appMessage, callback);
}

private void showDialog(@NonNull final AppMessage appMessage, @NonNull final AGConnectAppMessagingCallback callback) {
View view = LayoutInflater.from(activity).inflate(R.layout.custom_view, null, false);
final AlertDialog dialog = new AlertDialog.Builder(activity).setView(view).create();
Button click = view.findViewById(R.id.click);
Button dismiss = view.findViewById(R.id.dismiss);
TextView id = view.findViewById(R.id.id);
id.setText("MessageID: " + appMessage.getId());
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// set button callback
callback.onMessageClick(appMessage);
callback.onMessageDismiss(appMessage, AGConnectAppMessagingCallback.DismissType.CLICK);
dialog.dismiss();
}
});

dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set button callback
callback.onMessageDismiss(appMessage, AGConnectAppMessagingCallback.DismissType.CLICK);
dialog.dismiss();
}
});
dialog.show();
dialog.getWindow().setLayout((getScreenWidth(activity) / 4 * 3), LinearLayout.LayoutParams.WRAP_CONTENT);
callback.onMessageDisplay(appMessage);
}

Call addCustomView to implement the custom layout.

CustomView customView = new CustomView(MainActivity.this);
appMessaging.addCustomView(customView);

Test in app message​

Obtain the Anonymous Application Identifier (AAID) of your test device, which is used to identify app instances running on mobile devices during app installation. Compared with device-level hardware IDs that cannot be reset, the privacy of AAID is much higher. We can obtain the AAID through coding.

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();
textView.setText(aaid);
Log.d(TAG, "getAAID success:" + aaid );

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.d(TAG, "getAAID failure:" + e);
}
});

Sign in to AppGallery Connect, go to Grow > App Messaging > Messages, find the message that you need to test, and click Test in the Operation column.

Click Add test user and enter the AAID in the test box.

Click Save. Check whether the test message can be properly displayed and operated.

For details, please refer to the development guide of App Messaging.