fbpx
Get In Touch
1201 3rd Avenue Seattle, WA 98101, US
(HQ) Av. Punto Sur 31, Tlajomulco de Zúñiga, Jal 45050, MX
Carrera 11B # 99 - 25, Btá, 110221, CO
Let's talk
hello@inmediatum.com
Ph: +1 (650) 603 0883
Sales attention M - F 9am - 5pm (CT)
Get support
Careers
Endless inspiration and meaningful work
See open positions
Back

Firebase Remote Config

Hi again! Eduardo Rodriguez here with a little explanation about Remote Config. Before we start, remember to check my previous post about Firestore.

Why Firebase Remote Config?

This is the scenario: We needed to build a dynamic survey with branching on the questions, this survey needed to be updated as many times as my boss needs, besides that, another requirement was that we needed to change some background colors and images on some of the screens and as a final requirement, some buttons needed to display an URL we didn’t have yet.

So, the first approach I came up with was to use a REST API method as a catalog for all those variables, sadly the backend team was a little bit busy at the time, so building an API was not an option.

After some research, I found the perfect solution: Firebase Remote Config.

A little explanation about Firebase Remote Config

Remote Config is part of Firebase SDK. This part of the SDK allows us to define a set of variables which can be updated through the firebase console at any time, you can define a variable as simple as a string or a number, or as complex as a JSON object. Using Google Analytics, we also can perform A/B testing and send different data using the filters that analytics provide. Pretty cool, right?

Some things before coding…

As always, before we start using a firebase tool, we need to make all the configurations required for firebase to work in our Android project. This basically means that we need to create a new project in the Firebase Console, register our app with firebase and add the configuration file provided by firebase to our project in Android Studio. If you are not familiar with this process, here is a link with all the information you need to do it.

Let’s code!

First things first. We need to add the remote config library to our app gradle file:

implementation 'com.google.firebase:firebase-config:19.0.3'

Now that we have our library ready to work, we need to define an XML file which is going to contain the default values, in case our app cannot obtain the values from the firebase console. So, under our res/xml folder, we are going to create remote_config_defaults.xml. It should look something like this:

<?xml version="1.0" encoding="utf-8"?>
    <defaultsMap>
        <entry>
            <key>some_json</key>
            <value>
                {"name": "values also can be JSON"}
            </value>
        </entry>
        <entry>
            <key>some_color</key>
            <value>#20242D</value>
        </entry>
        <entry>
            <key>url_key</key>
            <value>some url</value>
        </entry>
    </defaultsMap>

Now that we have our configuration file, we need to initialize remote config:

FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(0)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);

After we have initialized our remote config instance, we just need to fetch the values. You should have in mind that this code should be called every time you need to update your values (you cand find more information about the intervals for updating in the Remote config documentation). This is the way you do it:

mFirebaseRemoteConfig.fetchAndActivate()
        .addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
            @Override
            public void onComplete(@NonNull Task<Boolean> task) {

                if (task.isSuccessful())
                    Utils.log(TAG, "Config params updated: ");
                else
                    Utils.log(TAG, "Fetch failed");
            }
        });

After this, all we have left to do is use our values:

mFirebaseRemoteConfig.getString("url_key");

What do you think?. Simple isn’t it? It seems like a simple tool, but trust me, it is really powerful too.

I hope this information helps you as much as it helped me.

Keep Coding!

Eduardo Rodriguez
Eduardo Rodriguez

We use cookies to give you the best experience. Cookie Policy