You can use App Messaging of AppGallery Connect 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 to certain products, provide hints to a game level, or recommend promotions of a restaurant. App Messaging even allows you to customize how your messages look and the way they will be sent, and define events for triggering message sending to your users when you want to engage your users in your app's activities.
In this codelab, you will build an app that integrates App Messaging. Messages that you create in AppGallery Connect will be displayed in your app.
To integrate this service, you must complete the following preparations:
If you are using DevEco Studio, you can integrate the SDK by using the Maven repository into your DevEco Studio project before development.
dependencies {
...
implementation "com.huawei.agconnect:agconnect-appmessaging-harmony:1.2.2.300"
...
}
In this codelab, you can create a layout page in your DevEco Studio project and design the UI according to the following figure, with the BUTTON DEBUG button to forcibly obtain message data from the AppGallery Connect server instead of from the local cache.
The sample code for the layout file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Button
ohos:id="$+id:button_debug"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="$string:btn_debug"
ohos:text_size="20vp"
ohos:top_margin="40px"
ohos:background_element="$graphic:background_ability_main"
/>
</DirectionalLayout>
Import header files to the MainAbilitySlice.java file of the project and add code for initialization.
import com.huawei.agconnect.appmessaging.ResourceTable;
import com.huawei.agconnect.appmessaging.AGConnectAppMessaging;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Button createShort = (Button) findComponentById(ResourceTable.Id_button_debug);
createShort.setClickedListener(component ->
debugMessage());
}
private void debugMessage() {
AGConnectAppMessaging.getInstance().setForceFetch()
}
}
Run your project in DevEco Studio, and switch the app to the background and then to the foreground. An in-app message is displayed, which is the same as that set in AppGallery Connect.
Congratulations! You have successfully built an app that integrates App Messaging and learned how to integrate the App Messaging SDK to your project and create in-app messages in AppGallery Connect.