Using Firebase Remote Config in Android
Firebase Remote Config is a cloud service provided by Firebase.
In previous article Getting Started With Firebase Android we have seen what are the features Firebase is providing to improve app performance and usability.
Firebase Remote config is the best feature from developer point of view, it allows you to perform some dynamic operation without updating the build. End user will get the latest changes without updating application to new version.
You can use Firebase Remote config to provide promotional offer banners to user without updating new build.
Firebase Remote Config allows you to set some layout behavior based on users language, locality, app version, gender etc.
Setup Firebase Remote Config
Step 1 : Create a project on Firebase Console .
Step 2 : After successful creation of your first project Click on Add Firebase to your Android app.
Step 3 : Add all the details of your project like Package name, App nickname, SHA – 1 key etc and download google.json file. Put that file in app/ folder
Step 4 : Add rules to your root-level build.gradle file
1 2 3 4 |
dependencies { ... classpath 'com.google.gms:google-services:3.0.0' } |
Step 5 : Add dependency to your module level build.gradle file and add apply plugin at bottom of file
1 2 3 4 5 |
dependencies { ... compile 'com.google.firebase:firebase-config:10.0.1' } apply plugin: 'com.google.gms.google-services' |
After adding google.json file and all setup you might get this error
Error:Execution failed for task ‘:app:processDebugGoogleServices’.
> Missing api_key/current_key object
It is a bug in google-services:3.0.0, but there is no need to worry about, once you add any service of the Firebase, replace the google.json file with newly created file. You can get new google.json file from Project Settings in Firebase console.
Step 6 : From Firebase Console setup your Remote Config by clicking on Remote Config from left menu.
Step 7 : Click on Add Your First Parameter will prompt a new dialog asking for parameter key and default value. After adding the parameter don’t forget to click publish changes.
Step 8 : Setup default value of config parameter in your android project in form of Map or XML resource File. Store your xml file inside res/xml. Here we have created default_config.xml file
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?><!-- START xml_defaults --> <defaultsMap> <entry> <key>background_color</key> <value>#000000</value> </entry> </defaultsMap> |
Note : Make sure that parameter name/key name must be the same as we have created in Firebase Console.
Step 9 : Get an instance of FirebaseRemoteConfig and set default values using setDefaults()
1 2 |
FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); firebaseRemoteConfig.setDefaults(R.xml.default_config); |
Step 10 : Fetch the value of background_color key by using firebaseRemoteConfig object
1 2 |
String bg_color = firebaseRemoteConfig.getString("background_color"); relativeMain.setBackgroundColor(Color.parseColor(bg_color)); |
Now you can build and run the application and you will see black background as it is default value we have provided in default_config.xml file
Now we will fetch the value from our server/console which we had already set to white(#FFFFFF). To fetch the value we need to use fetch() which will get latest value of all the parameters available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
firebaseRemoteConfig.fetch().addOnCompleteListener(this, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { // Once the config is successfully fetched it must be activated before newly fetched // values are returned. firebaseRemoteConfig.activateFetched(); String bg_color = firebaseRemoteConfig.getString("background_color"); relativeMain.setBackgroundColor(Color.parseColor(bg_color)); } else { Log.d("TAG", "Fetch failed"); } } }); |
As you can see in above code after fetching values successfully we have used firebaseRemoteConfig.activateFetched(); which will activate all the fetched values and new values will be reflected to our existing configuration.
Not only just background color, you can also use this feature to show some promotional banner, just fetch Boolean value from server and based on that show some dialog which describes offer you are providing.
Remote Config Example
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019