[Huawei Developers] Basavaraj.navi:
Introduction​

If are you new to this application, please follow my previous articles

In this article, I will explain what is Huawei Remote configuration? How does Huawei Remote Configuration work in Android? At the end of this tutorial, we will create the Huawei Remote Configuration Android Pygmy collection application.

In this example, I am Enabling/Disabling Export report to PDF using remote configuration. When Export PDF is enabled, user can Export report otherwise user can’t export report to PDF format instead user can just see the report in the application. And also generate PDF every 12 hour using android work manager.

What is Huawei Remote Configuration?

Huawei Remote Configuration is cloud service. It changes the behaviour and appearance of your app without publishing an app update on App Gallery for all active users. Basically, Remote Configuration allows you to maintain parameters on the cloud, based on these parameters we control the behaviour and appearance of your app. In the festival scenario, we can define parameters with the text, colour and images for a theme which can be fetched using Remote Configuration.

How does Huawei Remote Configuration work?

Huawei Remote Configuration is a cloud service that allows you to change the behaviour and appearance of your app without requiring users to download an app update. When using Remote Configuration, you can create in-app default values that controls the behaviour and appearance of your app. Then, you can later use the Huawei console or the Remote Configuration to override in-app default values for all app users or for segments of your user base. Your app controls when updates are applied, and it can frequently check for updates and apply them with a negligible impact on performance.

In Remote Configuration, we can create in-app default values that controls the behaviour and appearance (such as text, color and image etc.) in the app. Later on, with the help of Huawei Remote Configuration, we can fetch parameters from the Huawei remote configuration and override the default value.

Integration of Remote configuration

1. Configure application on the AGC.

2. Client application development process.

Configure application on the AGC

Follow the steps

Step 1: We need to register as a developer account in AppGallery Connect. If you are already developer ignore this step.

Step 2: Create an app, refer to Creating a Project and Creating an App in the Project

Step 3: Set the data storage location based on current location.

Step 4: Enabling Remote configuration. Open AppGallery connect, choose Grow > Remote confihuration

Step 5: Generating a Signing Certificate Fingerprint.

Step 6: Configuring the Signing Certificate Fingerprint.

Step 7: Download your agconnect-services.json file, paste it into the app root directory.

Client application development process

Follow the steps

Step 1: Create flutter application in the Android studio (Any IDE which is your favorite).

Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.6.1.300'
// For WorkManager.
implementation 'android.arch.work:work-runtime:1.0.1'

Root level gradle dependencies

maven { url ‘https://developer.huawei.com/repo/’ }
classpath ‘com.huawei.agconnect:agcp:1.4.1.300’

Add the below permissions in Android Manifest file.

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>

To achieve Remote configuration service example, follow the steps.

1. AGC Configuration

2. Build Flutter application

Step 1: AGC Configuration

1. Sign in to AppGallery Connect and select My apps.

2. Select the app in which you want to integrate Huawei Remote configuration Service.

3. Navigate to Grow > Remote configuration > Enable

Step 2: Build Android application

In this example, I am Enabling/Disabling Export report to PDF feature from remote configuration. When export PDF feature is enabled, user can export report to the PDF. Otherwise user can’t export report.

Basically, Huawei Remote Configuration has three different configurations as explained below.

Default Configuration: In this configuration default values defined in your app, if no matching key found on remote configuration sever than default value is copied the in active configuration and returned to the client.

AGConnectConfig config = AGConnectConfig.getInstance();
Map<String, Object> map = new HashMap<>();
map.put(“test1”, “test1”);
map.put(“test2”, “true”);
map.put(“test3”, 123);
map.put(“test4”, 123.456);
map.put(“test5”, “test-test”);
config.applyDefault(map);

Fetched Configuration: Most recent configuration that fetched from the server but not activated yet. We need to activate these configurations parameters, then all value copied in active configuration.

Config.fetch().addOnSuccessListener(new OnSuccessListener<ConfigValues>() {
@Override
public void onSuccess(ConfigValues configValues) {
config.apply(configValues);
// Use the configured values.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
}
});

Active Configuration: It directly accessible from your app. It contains values either default and fetched.

Fetch Parameter value

After default parameter values are set or parameter values are fetched from Remote Configuration, you can call AGCRemoteConfig.getValue to obtain the parameter values through key values to use in your app.

Config.fetch().addOnSuccessListener(new OnSuccessListener<ConfigValues>() {
@Override
public void onSuccess(ConfigValues configValues) {
config.apply(configValues);
isPDFGenerateEnabled = configValues.getValueAsBoolean(“enable_pdf_generate”);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
}
});

Resetting Parameter Values

You can clear all existing parameter using below function.

Config.clearAll();

What all can be done using Huawei remote configuration

Displaying Different Content to Different Users: Remote Configuration can work with HUAWEI Analytics to personalize content displayed to different audiences. For example, officeworkers and students will see different products and UI layouts in an app.

Adapting the App Theme by Time: You can set time conditions, different app colors, and various materials in Remote Configuration to change the app theme for specific situations. For example, during the graduation season, you can adapt your app to the graduation theme to attract more users.

Releasing New Functions by User Percentage: Releasing new functions to all users at the same time will be risky. Remote Configuration enables new function release by user percentage for you to slowly increase the target user scope, effectively helping you to improve your app based on the feedback from users already exposed to the new functions.

Features of Remote configuration

1. Add parameters

2. Add conditions

1. Adding Parameters: In this you can add parameter with value as many as you want. Later you can also change the value that will be automatically reflected in the app. After adding all the required parameters, let’s release the parameter.

2. Adding condition: This feature helps developer to add the conditions based on the below parameters. And conditions can be released.

App Version

OS version

Language

Country/Region

Audience

User Attributes

Predictions

User Percentage

Time

App Version: Condition can be applied on app versions. Which has four operator Include, Exclude, Equal, Include regular expression. Based on these four operators you can add conditions.

OS Version: Using the developer can add condition based on android OS version.

Language: Developer can add the condition based on the language.

Country/Region: Developer can add condition based on the country or region.

User percentage: Developer can roll feature to users based on the percentage of the users between 1-100%.

Time: Developer can use time condition to enable or disable some feature based on time. For example if the feature has to enable on particular day.

After adding required condition, release all the added conditions.

If (isPDFGenerateEnabled){
exportReport.setVisibility(View.VISIBLE);
}else {
exportReport.setVisibility(View.GONE);
}

exportReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPDFGenerateEnabled) {
try {
List<DailyCollectionEntity> entities = PygmyDatabase.getDatabase(getActivity()).dailyCollectionDao().findCollectionsByDate(formattedDate);
List<String> headers = new ArrayList<>();
headers.add(“Shop Name”);
headers.add(“Account Number”);
headers.add(“Account Holder Number”);
headers.add(“Today’s Balance”);
headers.add(“Total Balance”);
if (entities != null && entities.size() > 0) {
String path = getStorageDir(“Date_” + formattedDate + “.pdf”);
PDFUtility.createPdf(getContext(), TodayCollectionFragment.this::onPDFDocumentClose, getTableData(entities, formattedDate), headers, path, true);
}
} catch (Exception e) {
AGConnectCrash.getInstance().recordException€;
e.printStackTrace();
Log.e(“TAG”, “Error Creating Pdf”);
Toast.makeText(getActivity(), “Error Creating Pdf”, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), “PDF Generation is disabled”, Toast.LENGTH_SHORT).show();
}
}
});

Now create work manager.

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.shea.pygmycollection.utils.FileUtils;

public class GenerateReportToPDFWorkManager extends Worker {
private static final String TAB = GenerateReportToPDFWorkManager.class.getSimpleName();
public GenerateReportToPDFWorkManager(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
FileUtils.generatePDF();
return Result.success();
}
}Copy codeCopy code
Now build the periodic work manager.

private PeriodicWorkRequest mPeriodicWorkRequest;
// Generate pdf every 12 hour
mPeriodicWorkRequest = new PeriodicWorkRequest.Builder(GenerateReportToPDFWorkManager.class,
12, TimeUnit.HOURS)
.addTag("periodicWorkRequest")
.build();
WorkManager.getInstance().enqueue(mPeriodicWorkRequest);

Result​

Tips and Tricks​

Add the dependencies properly.

Add internet permission.

If required add condition properly.

Conclusion​

In this article, we have learnt integration of Huawei Remote configuration, how to add the parameters, how to add the Conditions, how to release parameters and conditions and how to fetch the remote data in application and how to clear the data in Android Pygmy collection application.

Reference​

Huawei Remote Configuration

Happy coding