Integrating HUAWEI In-app message together with Google In-app message in single Application

RChancha
6 min readAug 7, 2020

--

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.

In-app image message

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.

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.

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:

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

14. Click Publish.

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.

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

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

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.

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:

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

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

--

--