June 9, 2020

How to set firebase remote config values in android

By Mohit Agrawal

Update your app behavior without publishing the new build using firebase remote config

Introduction

Have you ever thought of changing some of your app features instantly without distributing an app update 🤔? Yes..right. Sometimes being a developer you might want to tweak some parameters in your app and want to test its behavior with live users without rolling out a new update. Here comes the firebase remote config to solve your issue. Firebase allows you to change your app configuration on the fly.

In this small tutorial, I will show you how to implement a firebase remote configuration in your android app. We will change the background color and a welcome message text using the firebase remote configuration tool.

Final output

Let me show you first that what we are going to build in this tutorial. So that

firebase_config_demo
Changing app background color and label from firebase config.

Steps to implement

Let’s see what are the general steps to implement this feature. I have optimized it to only 3 steps which cover all the parts. These steps are:

  • Integrate Firebase remote configuration library in your android project. Also, we need to connect the app to the firebase server.
  • Configure the firebase portal with keys and value.
  • Get the value from firebase and apply the logic which has to do on retrieved data.

Implementation

Now at this stage, you must have got the basic idea about the remote config feature. Let’s start with the implementation part so that it be more clear to you.

Step 1: Create a new android project with an empty activity. The first thing we will do is to integrate the firebase library into our brand new android project. Android studio makes this step very simple and automated.

Go to the Tools -> Firebase -> Remote Config -> click on Set up Firebase Remote Config. This will automatically implement all the required firebase plugins. In order to connect the android app to the firebase server, it will prompt the browser for firebase authentication. Just go with the flow and give the details it is asking for.

firebase_remoteconfig_implement

Step 2: We are done with the android studio part. Now we will configure the firebase portal with required keys and their values.

Go to the firebase website and open the same project which you have linked in the previous step. Find the option Remote config at the left side menu and click on it as shown in the gif below.

remote_config_keys

As you can see I have already added two remote config keys and value. The first one is “greeting_label” and the second one is “color_background”. Similarly, You also set them by clicking on the Add parameter button.

firebase remote config

Parameter key should be unique through out your project. The same key will be used in the code to get the data as well.

Step 3: This is going to be the last step. Now we will write the code in android studio to fetch the data from the firebase remote config server.

Let’s build a simple UI first. Design a plain text view and give it id “textview_greeting”. XML code for UI.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview_greeting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="textview_greeting"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Go to MainActivity.kt and write the code for the firebase config like this.

import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    private var mFirebaseRemoteConfig: FirebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
    private var GREETING_CONFIG_KEY = "greeting_label"
    private var COLOR_CONFIG_KEY = "color_background"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textview_greeting.text = "text view"

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

        getRemoteConfigValues()
    }

    private fun getRemoteConfigValues() {
        //here I have set the cache expiration duration to 1 hour
        //It means app will refresh after every 1 hour to check
        // if some changes are there in remote config
        var cacheExpiration: Long = 3600

        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() {
        textview_greeting.setText(mFirebaseRemoteConfig?.getString(GREETING_CONFIG_KEY))
        val remoteValueBackground = mFirebaseRemoteConfig?.getString(COLOR_CONFIG_KEY)
        if (remoteValueBackground.isNotEmpty()) {
            main_layout.setBackgroundColor(Color.parseColor(remoteValueBackground))
        }
    }
}

Try to run your app. If you have done everything correct till here then you should be able to get values from firebase. Please let me know in the comment section if you stuck somewhere.

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!

android android beginners tutorial android tutorial Animation API call API call in iOS async asynchronous cardview custom switch custom ui dark web dark web links deep web deep web links Dynamic gradient view facebook messenger firebase hosting firebase hosting tutorial firebase remote config firebase tutorials flutter flutter for beginner flutter tutorial flutter UI tutorial hacking hacking tutorial hashcat iOS ios tutorial ios ui tutorial kotlin listview in flutter Lottie animation Messenger for kids review onion links Roll the Dice app swift tech news tor tor browser tor links UISwitch wifi wifi hacking


FAQs

  1. java.net.SocketException: socket failed: EPERM (Operation not permitted) [SOLVED]

    To solve this error I uninstalled the app and run it again.

  2. Is firebase remote config free?

    Firebase remote config is one of the feature of firebase and it is completely free.

  3. Why to use firebase remote config?

    Sometimes being a developer you might want to tweak some parameters in your app and want to test its behavior with live users without rolling out a new update. Here comes the firebase remote config to solve your issue. Firebase allows you to change your app configuration on the fly.

  4. Firebase remote config limit

    You are allowed to use upto 2000 parameters and upto 500 conditions within a single project. The total length of parameter value strings within a project cannot exceed 800,000 characters.

  5. Firebase remote config not updating or working

    It may not update if have these type of mistakes. Make sure you to publish your changes after updating key and value on firebase portal. Also make sure your device is connected to the internet.
    Sometime It also doest not work if your SHA certificate is not matching.

  6. Firebase remote config update time

    You set the update time like this. Here in every one hour, it will check for updated data.
    refresh_time