Skip to main content

Firebase Remote Config Step by step Guide


 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.









Firebase Remote Config
Success Case










Failure Case

Source Code of this demo app are available on GitHub you can download form here.

Comments

  1. […] 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

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Create Custom EditText View in Android

We use the EditText for taking the input from the user and use it at several places in our project. We required to do lots of customization for each time and there are lots of redundant code we write. Writing and managing these redundant codes is very difficult for example if we want to change the look and feel of the view them we need to modify it at each place where our EditText is getting used. So to avoid these kinds of the problem we can create our own Custom EditText View by just. The EditText view is just an extension of the TextView with lots of editing option and properties that required for the user input. How To Create Custom EditText View For creating the Custom EditText we need to extend the AppCompatEditText and override all three constructors of the view as given below. import android.content.Context; import android.graphics.Typeface; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatEditText; import android.util.AttributeSet; public