Getting Started with Firebase RemoteConfig in Android

Suppose you are an Android developer and you have your app published on the Play Store. Everything is going right but what you noticed that there are a number of users that are demanding for some changes in the UI of your app. Suppose a user is demanding for small text in the app, another user is demanding for some different background for the app and so on and so forth. So being a good Android developer and a good businessman (suppose you are earning a lot from the app), you are thinking of a solution to satisfy the needs of your user. Before reading the next part of this blog, please think of for the solution.

So, one solution can be the update of your App. An update will bring some new features to the app but do you think this will really address the basic problem that your users want to remove. Also if you have updated the app and it is not sure that your every user will update your app from the play-store. So, is there any way to make some changes in the app(update the app) without posting it on play-store?? The reason why I am asking this is, for a small change in an app suppose the change in font size should not be done in an app update on the play-store. If your app is having some drastic or important change, then you should put the new version of your app on the play-store.

Yes, you got it right. In this blog, we will learn how to make certain changes in your app without updating the newer version of the app on the play-store. Hold on, what did you say? Updating an app without putting the update on the play-store? Is it possible??

The answer is yes, a big YES. One way of doing so is the Firebase Remote Config :) So, in this blog, we will learn about the Firebase Remote Config feature that will allow you to make some changes to your app remotely. In this blog we will discuss:

  • What is Firebase Remote Configuration?
  • What does Firebase Remote Configuration do?
  • How does it work?
  • Remote Config process flowParameters and Conditions in Remote Config
  • An example of Firebase Remote Config
  • Policies and Limits
  • Conclusion

So, let’s get started.

What is Firebase Remote Configuration?

Firebase Remote Config is a cloud service offered by Firebase that lets you change the design or the appearance of your app without publishing an update of your app on the play-store or any other app store. Now, it is not a matter of concern that your users have updated the app or not, to have the latest feature. All they need to do is having a stable internet connection and the changes that you have made to the app will be published in your app installed in their mobile phones. The best part of Firebase Remote Config is that you can manage the change from the Firebase Console or form Remote Config REST.

What does Firebase Remote Configuration do?

The next question that comes to our mind is, what does Firebase Remote Configuration do? The answer to this question is:

  • You can publish the changes in to your app very quickly. For example, if you are having a coffee shop app and there is a TextView displaying the opening and closing time of the coffee shop. Suppose, for a period of 5 days the timing of the coffee shop is changed. So, in order to update the time for those 5 days, you shouldn’t release an update for the app, instead, you can use Firebase Remote config for the same purpose.
  • The Second benefit of using Remote Config is that you can change the appearance of your app according to your users. Suppose, you are having the information of the gender of the user, then you can use some light color as the background of the app for the female users and can use the dark theme for the male users.
  • Another benefit of using Remote Config is that you can apply some change in your app for a particular percentage of your users for testing. Suppose, you want to change the background color of your app and want to know if users will like this color or not. To do so, you can use Remote Config to apply changes for a particular group of users and after collecting the suggestions you can make the required changes for all users in your app update.

Remote Config process flow

By using Remote Config, you can update certain values in you app like some TextView or some ImageView. So, we will give some predefined value to each of these items that are to be updated using Remote Config. In a simpler way, we are giving some id to each and every component in our that we will be updated using the Remote Config. Remote Config requires 4 steps to publish an update:

  1. Define Remote Configuration: In this step, we will define or identify those parts of the app that we need to update by using Remote Config. We will assign them some kind of id to uniquely identify them.
  2. Assign Defaults for Parameters: Before having the changed UI in the app, there must be some default look for the app that the user will see until we get the Remote Config items value. This can be done by using the setDefaults() method from the SDK.
  3. Get the value from Remote Config: After having the default value, we will get the values from the Remote Config by using the get() method. After that, we apply our logic that must be performed on the retrieved value.
  4. Update value in Remote Config: After that, whenever you want to make some changes in your app, then simply update the values of the parameters stored in the Remote Config by going on to the Firebase Console.

Parameters

Parameters are key-value pairs that are defined in the Remote Config. These are used to make changes in the app. For example, if the key of a TextView in your app is my_text_color and the value is #000000 then this will change the color of your text to black.

Similarly, in our app, we have some default key-value pair that is used to set the default configuration of our app. You can change the parameters from the Firebase Console.

Conditions

Conditions are something that is applied to Remote Config to target a particular section of your users. For example, if you want to give some features to the paid members of your app then you can do so by applying some condition in the Remote Config and same APK will look different from those who are using the free version of it.

You can update more than one values at a time from the Remote Config. The updated values will be shown in bold but make sure to click on “Publish Changes” button to make your change live.

A simple flow of how things happen in Remote Config is shown below:

Picture courtesy: Firebase

An example of Firebase Remote Config

So, let’s have an implementation of the Remote Config. In this example, we have one ImageView in between two TextView. We will change the TextView and background of the activity. So, let’s get started:

Step 1: Create a new project in Android Studio

Step 2: Click on Tools > Firebase > Remote Config > Set up Firebase Remote Config > Connect to Firebase

Make Firebase project and click Connect to Firebase.

Step 3: Click on Add Remote Config to your app and then click on Accept Changes. This will add the Remote Config dependencies to your app.

Step 4: Now, you are done with the Android Studio part, now visit the Firebase Console website and login with the same email id as used in the Android Studio while connecting to Firebase. After that, select your project and open the project overview section. Now scroll down to the end of the page and click on See all Firebase features. Then in the Grow section, click on Remote Config. This is the place, where you can add your Key and Value. Add your key and value here. Please note that this key should be the same that you will use in your app as a default configuration. So, make sure you remember this key. In my case the keys and values are:

key: app_sub_text_view value: Hello! MindOrks

key: app_main_text_view value: Remote Config

key: app_background_color value: #0F9D58

The dashboard will look something like this:

Phew, so many steps, but now you are done with the Setup part of the Remote Config. Now, let’s move on to the coding part.

Step 5: Add your UI in the activity_main.xml file, the code of activity_main.xml file of my project is:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#4285F4"
        tools:context=".MainActivity"
        android:orientation="vertical">

    <TextView
            android:id="@+id/remote_value_main_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Firebase Remote Config"
            android:textAlignment="center"
            android:textSize="32sp"
            android:layout_margin="16dp"
            android:textColor="#FFFFFF"
            android:textStyle="bold"/>

    <ImageView
            android:id="@+id/remote_value_iv"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_gravity="center"
            android:layout_marginTop="32dp"
            android:background="@drawable/androidimage"/>

    <TextView
            android:id="@+id/remote_value_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="Learning Firebase Remote Config"
            android:textSize="24sp"
            android:textColor="#FFFFFF"
            android:layout_marginTop="24dp"
            android:textStyle="bold"/>
</LinearLayout>

Step 6: In the res/ folder make an xml directory of type xml and inside that xml directory make a file named firebase_defaults.xml. In this file, you will put your default configuration code.

Make sure that keys used here is same as that used in Remote Config dashboard.

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
        <key>app_background_color</key>
        <value>#4285F4</value>
    </entry>
    <entry>
        <key>app_main_text_view</key>
        <value>Firebase Remote Config</value>
    </entry>
    <entry>
        <key>app_sub_text_view</key>
        <value>Learning Firebase Remote Config</value>
    </entry>
</defaultsMap>

Step 7: In the MainActivity.kt, add the below code for Remote Config:

class MainActivity : AppCompatActivity() {


    private var APP_BACKGROUND_COLOR_KEY = "app_background_color"
    private var APP_MAIN_TEXT_VIEW = "app_main_text_view"
    private var APP_SUB_TEXT_VIEW = "app_sub_text_view"

    private var mFirebaseRemoteConfig: FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance()

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        val mainLayout = layoutInflater.inflate(R.layout.activity_main, null)
        setContentView(mainLayout)

        title = "Firebase Remote Config Demo"

        //Enable Debug mode for frequent fetches
        val configSettings = FirebaseRemoteConfigSettings.Builder()
            .setDeveloperModeEnabled(BuildConfig.DEBUG)
            .build()
        mFirebaseRemoteConfig?.setConfigSettings(configSettings)

        //setting the default values for the UI
        mFirebaseRemoteConfig?.setDefaults(R.xml.firebase_defaults)

        //getting the Remote values from Remote Config
        getRemoteConfigValues()

    }

    private fun getRemoteConfigValues() {
    remote_value_main_tv?.setText(mFirebaseRemoteConfig?.getString(APP_MAIN_TEXT_VIEW))
        remote_value_tv?.setText(mFirebaseRemoteConfig?.getString(APP_SUB_TEXT_VIEW))
        //here we have set the cache expiration to 2 hrs
        //by setting the cacheExpiration, you app will refresh after every 2hour to check
        // if some changes are there in remote config
        var cacheExpiration: Long = 7200

        mFirebaseRemoteConfig?.fetch(cacheExpiration)?.addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    Toast.makeText(applicationContext, "Fetch Succeeded", Toast.LENGTH_SHORT).show()
                    mFirebaseRemoteConfig?.activateFetched()
                } else {
                    Toast.makeText(applicationContext, "Fetch Failed", Toast.LENGTH_SHORT).show()
                }
                //changing the textview and backgorund color
                setRemoteConfigValues()
            }
    }

    private fun setRemoteConfigValues() {
        val remoteValueMainText = mFirebaseRemoteConfig?.getString(APP_MAIN_TEXT_VIEW)
        val remoteValueSubText = mFirebaseRemoteConfig?.getString(APP_SUB_TEXT_VIEW)
        val remoteValueBackground = mFirebaseRemoteConfig?.getString(APP_BACKGROUND_COLOR_KEY)
        if (!remoteValueBackground.isNullOrEmpty()) {
            remote_value_main_tv?.text = remoteValueMainText
            remote_value_tv?.text = remoteValueSubText
            main_layout?.setBackgroundColor(Color.parseColor(remoteValueBackground))
        } else
            remote_value_main_tv?.text = "Failed to load values"
    }
}

Now, run you app and see the changes. Update some values in the Remote Config from the Firebase Console and again restart the app. You will see the changes.

Note: If you get an error: Default FirebaseApp is not initialized in this process then in the build.gradle file of project level, change the google-services version to 4.0.0

classpath 'com.google.gms:google-services:4.0.0'

Policies and Limits

Before using Firebase Remote Config, look at to some policies and limits.

Policies

  • If your update requires some user authorization, then you should avoid using the Remote Config to update the app.
  • Never store any confidential data of your app in the Remote Config.

Limits

  • You can have 2000 Remote Config parameters in a Firebase project.
  • You can store up to 300 versions of your Remote Config and Firebase will store those versions for a maximum time period of 90 days.

Conclusion

So, in this blog, we learned about Firebase Remote Config, that is used to make small updates in your app without publishing the update on the play-store. Remote Config is very useful when we have some minor updates in our app but for some major updates requiring some authorization, we should avoid using Remote Config. Inthe end, we also saw one implementation of Remote Config. Hope you enjoyed this blog. So, try implementing some more features of Remote Config to your app. You can take reference from the official website of Remote Config.

See you in the next blog, till then, Keep Learning :)

Team MindOrks :)