You must have seen the Apps on your mobile changing their color scheme on special occasions or changing their appearance in different manners without any prior App Update. Well, if you want to do something same with your own App Firebase has a service for you called Firebase Remote Config. It helps you change the behavior and appearance of your App without rolling out a new version of your App. Without Firebase Remote Config, you have to write a lot of code from scratch to provide such user experience. In this post, we are going to learn more about Firebase Remote Config and how to implement it in your Android App.
What is Firebase Remote Config?
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.
Some Use Cases for Firebase Remote Config
Some of the functionalities you can achieve with Firebase Remote Config are given below.
- Changing the color scheme of your App based on different occasions like festivals, sale and many more.
- Showing different quotes (one-liners or phrases) based on your App’s Context.
- Showing a screen to update your Apps, in case an Update is available, as the App starts.
I am going to use the second use case to explain the implementation of Firebase Remote Config in Android and will write a separate post to explain how to implement third use case.
How does Firebase Remote Config work?
The working or the implementation path of Firebase Remote Config can be understood with below-given points.
- First of all, decide which aspects of your app’s behavior and appearance you want to be able to change using Firebase Remote Config. Then translate these into the parameters that you will use in your app i.e decide the different key-value, parameter & its value, pairs.
- Set the in-app default values for Remote Config parameters, same key-value pairs we discussed in above point, using setDefaults() method. We will see the implementation of this method later in this post.
- Firebase Remote Config provides a client library for each platform (Android, iOS) that handles important tasks like fetching parameter values and caching them, while still giving you control over when new values are activated so that they affect your app’s user experience. This lets you safeguard your app experience by controlling the timing of any changes. The Firebase Remote Config client library get methods provide a single access point for parameter values. Your app gets service-side values using the same logic it uses to get in-app default values, so you can add the capabilities of Firebase Remote Config to your app without writing a lot of code.
- In simple terms, Firebase Remote Config, first of all, tries to fetch the values from the service and leave it up to you when you want to activate those values. The client library provides a get method to use the parameter values. This get method returns the value fetched from the service if activated, otherwise returns the default value defined at the client side. You can refer the image given below for more clarity.
- At last, to override in-app default values, use the Firebase console or the Remote Config REST API to create parameters with the same names as the parameters used in your app. For each parameter, you can set a service-side default value to override the in-app default value, and you can also create conditional values to override the in-app default value for app instances that meet certain conditions. We will discuss more about conditional values in next section.
Firebase Remote Config Parameters and Conditions
As stated above, using the Firebase console, you can create new default values for your parameters, as well as conditional values that are used to target groups of app instances i.e you can use different values for the same parameter(key) for different user groups. In the following paragraphs, we learn more about conditions, rules, conditional values and how the conditional values are prioritized.
Conditions, rules, and conditional values
A condition is used to target a group of app instances, and conditions are made up of one or more rules that must all evaluate to true for the condition to evaluate to true for a given app instance. If the value of a rule is undefined (for example, when no value is available), that rule will evaluate to false. The different rule types available in Firebase Remote Config are given below in this post.
A conditional value consists of a condition and a value that is fetched by app instances targeted by that condition. A parameter can have multiple conditional values that use different conditions.
Parameter value priority
A parameter might have several conditional values associated with it. The following points determine which value is fetched from the Remote Config Server, and which value is used in a given app instance at a particular point in time.
Server-side parameter values are fetched according to the following priority list
- First, conditional values are applied, if any have conditions that evaluate to true for a given app instance. If multiple conditions evaluate to true, the first (top) one shown in the Firebase console UI takes precedence, and conditional values associated with that condition are provided when an app fetches values from the service. You can change the priority of conditions by dragging and dropping conditions in the Conditions tab.
- If there are no conditional values with conditions that evaluate to true, the service-side default value is provided when an app fetches values from the service. If a parameter doesn’t exist in the service, or if the default value is set to No value, then no value is provided for that parameter when an app fetches values.
In your app, parameter values are returned by get methods according to the following priority list
- If a value was fetched from the service and then activated, the app uses the fetched value. Activated parameter values are persistent.
- If no value was fetched from the service, or if values fetched from the service have not been activated, the app uses the in-app default value.
- If no in-app default value has been set, the app uses a static type value (such as 0 for int and false for boolean).
The image given below describes various rules type supported. This image has been taken from Firebase docs.
Now that we have understood how Firebase Remote Config works, we will see how to implement it in your Android App. The next section describes the implementation of Firebase Remote Config in Android step by step.
Implementation of Firebase Remote Config in Android
As said earlier in this post, we will implement second use case to learn how to implement Firebase Remote Config in Android. Follow steps given below.
Before following the points given below there are some pre-requisites that you need to have completed.
- You should have connected your Android App to Firebase i.e. You must have created a project on firebase console and connected it your Android Application. If you have not, then follow the instructions on Firebase Android Setup page and then return to this article.
- You should be developing your Android Project on Android Studio
Add the below-given dependency to the app-level build.gradle file in your Android project.
Set In-App Default Values
We will set in-app default parameter values in the Remote Config object so that your app behaves predictably before it fetches values from the Remote Config service.
- First, we will define a set of parameter names and default parameter values in an XML resource file stored that will be in your app’s res/xml folder. In our case, we will save quotes and their author’s name. Copy the content from the below-given xml file and paste it in your file or you can define your own.
- Then get the singleton Remote Config object, using the FirebaseRemoteConfig.getInstance() method. This singleton object is used to store in-app default parameter values, fetch updated parameter values from the service and control when fetched values are made available to your app. Initialize this
singleton object in your App’s main activity file.
- Now add the values defined in xml file to the Remote Config object using setDefaults() method. Refer the code below for your reference.
Get Parameter Values
As discussed earlier, the firebase remote config client library provides a single access point to both fetch values from the service or to return in-app default values. In Android to get these values, we can call the method listed below that maps to the data type expected by your app, providing the parameter key as an argument.
- getBoolean(), getByteArray(), getDouble(), getLong(), getString()
- As we are using firebase remote config to store quotes of famous persons, we will use getString() method.
- Now set these parameter values to your App’s views. Refer the code below.
Fetch and Activate Values from the Service
- Now to fetch parameter values from the Remote Config service, call the fetch() method. Any values that you set in the Remote Config service are fetched and cached in the Remote Config object.
- To make fetched parameter values available to your app, call the activateFetched() method.
- For our requirement, paste the below-given code in the MainActivity file described in Step 2.
Because these updated parameter values affect the behavior and appearance of your app, you should activate the fetched values at a time that ensures a smooth experience for your user, such as the next time that the user opens your app.
Set Parameter Values in the Service
- In the Firebase console, open your project.
- Select Remote Config from the menu to view the Remote Config dashboard.
- Define parameters with the same names as the parameters that you defined in your app. For each parameter, you can set a default value (which will eventually override the corresponding in-app default value), and you can also set conditional values which we already discussed. Refer the images below.
- Remote Config caches values locally after the first successful fetch. By default, the cache expires after 12 hours, but you can change the cache expiration for a specific fetch by passing the desired cache expiration to the fetch method. If the values in the cache are older than the desired cache expiration, Remote Config will request fresh config values from the service. If your app requests fresh values using fetch several times, requests are throttled and your app is provided with cached values.
- Throttling is done from within the SDK. An app can fetch a maximum of 5 times in a 60-minute window before the SDK begins to throttle and returns FirebaseRemoteConfigFetchThrottledException.
- During app development, you might want to refresh the cache very frequently (many times per hour) to let you rapidly iterate as you develop and test your app. To accommodate rapid iteration on a project, you can temporarily add a FirebaseRemoteConfigSettings object with isDeveloperModeEnabled set to true to your app, changing the caching settings of the FirebaseRemoteConfig object. Refer the code below for development environment.
A major part of this post contains material from Firebase docs. In this post, I have tried to make the implementation of Firebase Remote Config as easy as possible for you.
Now I have described all the steps required to implement Firebase Remote Config in Android. To learn and explore more about Firebase Remote Config, read through Firebase docs below.
If you face any problem in the tutorial above, you can always leave a reply or reach me through the chat option given in bottom right corner of the window.