Advertisement

How to setup Firebase Remote Config in an Android App

With Google Firebase Remote Config, you can change values of parameters aka variables inside your android app from Firebase Console any time you desire. You can change appearance, behavior etc. of your app without publishing an update. Typical examples include :

  1. Change color of app based on season.
  2. Change welcome message.
  3. Customize app behavior based on specific type of users. etc.

Step by step procedure to use remote config functionality of Google Firebase is as follows.

Use Case :

Suppose you have a textview in which you want to show a Quote. You want to change this quote remotely. Or you can also display some greeting message to particular group of users on various occasions like Christmas or Diwali or Eid. Lets decide parameter name for the same as “current_greeting”.

Create new parameter in Firebase Console for our project. Check out following video.

Step one :

Add Google Firebase to your app as per this procedure. Click on following link.

How to add Google Firebase to an Android App

Step Two :

Open app level gradle build file and add following line inside dependencies section. Then press Sync Now Button to sync the gradle files.

Step Three:

Add following variables at the top of Main Activity Class or any other activity in which you want greeting or quote text to appear.

In code above, FirebaseRemoteConfig is a singleton class. cacheExpiration variable sets time for rechecking the Firebase server for new values for our parameter. This value is in seconds. Above we have set Cache Expiration to 1 Hour. It can be set to any number of hours or days.

Step Four :

Default values for our parameters must be set inside the app so that if remote values are not available, default values can be used. Default values can be specified in an xml file inside res/xml folder.

Here we are setting name of defaults xml file as remote_config_defaults.xml

Step Five:

Inside onCreate() method of Main Activity, add following code.

In above code, we do following things.

  1.  First get singleton instance of FirebaseRemoteConfig .
  2. Build FirebaseRemoteConfigSettings and add developer mode so that cache can be refreshed every time. We can remove it after we finish testing our app.
  3. Set default values for our parameters using xml file we created earlier.
  4. Set cacheExpiration to zero if we are in developer mode.

Step Six:

Add following code to onCreate() method of that Activity or onCreateView() method of that fragment.

Explanation of above code : 

First get handle to our Greetings TextView. Here we are displaying greeting text view inside a fragment.

greetingView = (TextView) fragmentview.findViewById(R.id.greeting_view);

Next set Greetings View text to default text set inside app.

greetingView.setText(activitycontext.mFirebaseRemoteConfig.getString(“current_greeting”));

Not fetch remote config parameters from Google Firebase Server and set on Complete listener. Inside this listener, if fetch task is successful, activate fetched parameter values.

activitycontext.mFirebaseRemoteConfig.activateFetched();

Then set Greetings Text View to fetched text.

greetingText = activitycontext.mFirebaseRemoteConfig.getString(“current_greeting”); 

insightView.setText(greetingText);

Thats it..!!

Run the App and you shall see the remotely configured parameter value inside the Text View.