Firebase remote config on Android

Firebase has become very necessary for Android developers today. All the features of the firebase are super. Real-time Databases, Cloud Messaging, Crashlytics are common in the Android world. One of the features I had never used from Firebase is Firebase Remote Config. At first, it seemed like a feature that is not necessary and without any value. During the development of a project, the question arises of how we can update the app without having to publish a new version of Playstore and making it possible for all users to have this change. In our case, we had to change the icons for Christmas and the color in some parts should be red. I said to the manager’s project that I read for a feature that firebase has for free(Project Managers love free services) but did not know if it was something ideal for the case since I did not take any feedback until I had to use it by myself.

What does Firebase Remote Configuration do?

Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update. When using Remote Config, you create in-app default values that control the behavior and appearance of your app. Then, you can later use the Firebase console or the Remote Config REST API to override in-app default values for all app users or for segments of your user base. Your app controls when updates are applied, and it can frequently check for updates and apply them with a negligible impact on performance.

How to use it?

First of all, we should import the library:

compile ‘com.google.firebase:firebase-config:11.8.0’

Or using Firebase Assistant, just click on the text: “Set Up Remote Config” and the above-mentioned library will be automatically added.

Add the parameters in Firebase Console.

Once we’ve opened the Firebase console, we’ll see two text fields:

“Parameter key” and “Default value” which we have to fill in and finally we should not forget to make “PUBLISH “to save our changes.

The Remote Config has recently added the option to save a value-by-key list by saving it to a json format.

Implement remote configuration in your app…

Now it is the time to implement Firebase Remote configuration in our application. First we need to build a .xml file in the XML folder.

If you look at it carefully the key is the same key that is in the console at the firebase. After we’ve set up everything we need to fetch the data from the firebase console.

The following class does this:

public class FirebaseConfig {
FirebaseRemoteConfig mFirebaseRemoteConfig;
FirebaseRemoteConfigSettings configSettings;
long cacheExpiration = 43200;
String imgUrl = "";
public FirebaseConfig() {
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
configSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG)
.build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);
mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);
}
public String fetchNewImage(Context context){
mFirebaseRemoteConfig.fetch(getCacheExpiration())
.addOnCompleteListener((Activity) context, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// If is successful, activated fetched
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
} else {
Log.d("","");
}
imgUrl = mFirebaseRemoteConfig.getString("new_image");
}
});
return imgUrl;
}
public long getCacheExpiration() {
// If is developer mode, cache expiration set to 0, in order to test
if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
return cacheExpiration;
}
}

In case we want to fetch a new image we just need to change the URL in the Firebase Console.

Conclusion

“Remote config” is very useful because we can make quick changes to the app without making an update on Playstore, I suggest to use it. We can also build mechanisms to make these types of changes in the app but I do not think we need to reinvent the wheel.

If you would like to get in touch, ping me via Github or LinkedIn.