Firebase Remote Config enables you to dynamically change the layout and behavior of your app without changing the coding of your App. Event its no requirement you to create any backend servers. Just you have to follow some easy steps your app change the layout behavior during the run time depending on the condition that you set from Google Firebase. Hence it is an extremely good solution provided by google to easily change the layout of your App at the run time.
Firebase Remote Config Prerequisites
For configuration of Firebase Remote Config, we required some prerequisites which need to be done. First of all for configuring the remote config you need to setup a Firebase account. Go to Firebase home page and register your self if not register yet or login to Firebase.
Then click on Get Started For Free for testing purpose and then click on Create New Project.
It will open a popup window and ask for project name and country, enter the project name and select the country and click on Create Project as given below.
Here you can see the dashboard of your Firebase home page. Here you have add your application into this project, So click on Add Firebase to your Android App option.
It open a popup and asks for your app package name and SHA1 key but SHA key is the optional for remote config so you can left in blank, click on ADD APP button for next action.
Here it prompt you to save google-services.json, store this file to your computer will use this later in our application. You can download it anytime after login into the firebase.
Click on the continue button to move to the next step, it will give you some configuration step for your app which need to be done for your app we will this when start creating our application.
Create Project for Firebase Remote Config
We are going to create our project now using Android Studio for our Firebase Remote Config Demo.
Steps to Create Project
- From Menu Bar Click on File
- Select New > New Project
- Put the Name of the project ( Name should be same as we have in Firebase Remote Config Project Dashboard)
- Package name should be also same as we have configured during Firebase Remote Config app addition
Follow the rest of the steps to complete the full steps. At the last steps, you should select the empty Activity and click on finish button for this Demo. The project will be get created and MainActivity.java will be open, do nothing here we have do the configuration first for firebase integration as provided in last steps of our app addition steps in firebase.
Set Remote Config JSON Configuration to your App
Did you remember we have saved the google-services.json configuration file during our app addition steps into firebase. Copy that file and paste it inside your application home app folder, in my case location as given below.
E:MyProjectDebugAndroid ExampleRemoteConfigapp
Dependency Configuration for Firebase Remote Config
There is some dependency which needs to be injected inside our project level build.gradle file. So configuring the dependency open the project build.gradle file and modify it as given below.
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.google.gms:google-services:3.0.0'
}
We also required configuration of the firebase library to our app level build.gradle file. So open your app level build.gradle file and put the below line under the dependencies section.
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-config:9.6.1'
The last step is to add the google service plugin to your project. So for the addition of this, we need to add below configuration at the end of your app level build.gradle files. and click on sync now.
apply plugin: 'com.google.gms.google-services'
So finally click on the sync button from top right corner to sync your project with the configuration change which we did in our build.gradle file. As of now we have completed our project level configuration. Hence in next section, we are moving into integration part of our app with firebase.
Remote parameter Configuration and Implementation
Till now we have learned that how to configure your app to work with Firbase and Remote configuration. In this part we will configure our remote parameter and test our app.
Login to Firebase and select your project and click on RemoteConfig at the bottom of your left menu bar.
Click on ADD YOUR FIRST PARAMETER here.
Enter your Parameter Key same as you are going to use in your app and default value that you want and click on ADD PARAMETER.
If you want you can add more parameter and modify an existing one from here, as of now we are taking only one parameter for our application.
Implementation of Remote Parameter to code
We are adding a textview to our main Activity xml files here so that we can show our remote value in this textbox in real case you can do whatever you want do with these remote parameter.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.debugandroid.remoteconfig.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_remote_config"
android:text="Remote Config Parameter Not set" />
</RelativeLayout>
Implementing the Firebase Remote Config
Open the MainActivity.java and modify it as given below.
package com.debugandroid.remoteconfig;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private FirebaseRemoteConfig mRemoteConfig;
private TextView txt_remote_config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing the RemoteConfig instance
mRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings firebaseRemoteConfigSettings =
new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(true)
.build();
// Define default configuration values. It can be used in case where
// config not fetched due to any issue
Map<String, Object> defaultConfigMap = new HashMap<>();
defaultConfigMap.put("remote_test_parameter", 20);
// Apply the configuration settings and default values of remote config.
mRemoteConfig.setConfigSettings(firebaseRemoteConfigSettings);
mRemoteConfig.setDefaults(defaultConfigMap);
txt_remote_config= (TextView) findViewById(R.id.txt_remote_config);
//calling the loadConfig Method to fetch the remote configuration
loadConfig();
}
private void loadConfig() {
long cacheExpiration = 3600; // we set here 1 hours in seconds
// If developer mode is enabled we need to reduce cacheExpiration to 0 so that
// every time our app fetch the config from remote server.
// remove the below line of if condition code in release version
if (mRemoteConfig.getInfo().getConfigSettings()
.isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
mRemoteConfig.fetch(cacheExpiration)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mRemoteConfig.activateFetched();
//calling the ApplyConfig method to apply the fetch configuration
applyConfig();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("RemoteConfig", "Error fetching config: " +
e.getMessage());
// On error we can apply different method or we can use the same as well
// As we have already set the default value
applyOnFailure();
}
});
}
private void applyOnFailure() {
String remote_value=mRemoteConfig.getString("remote_test_parameter");
txt_remote_config.setText("Remote Config fetch failed, Setting Default value is:" + remote_value);
}
private void applyConfig() {
String remote_value=mRemoteConfig.getString("remote_test_parameter");
txt_remote_config.setText("Remote Config fetch, value is:" + remote_value);
}
}
Setting permission to your Project
We required internet access to work the Firebase Remote Config, So add the required permission inside your project AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />We have done with completed the integration of the Firebase Remote Config Parameter, you can test your app now. Here is the sample output of our remote config demo app which we have created in this tutorial. One snap for Success Case and with Failure case, you can create the scenario by enabling and disabling the internet service.
![]() |
Success Case |
![]() |
Failure Case |
Source Code of this demo app are available on GitHub you can download form here.
[…] any android app to google to get the configuration file then you can follow the process defined in Firebase Remote Config Prerequisites section or our earlier post. You have to just change the API name at final stage as given […]
ReplyDelete