8

Using Firebase Remote Config in Android - Ravi Rupareliya

 2 years ago
source link: https://www.ravirupareliya.com/blog/using-firebase-remote-config-in-android/?amp%3Butm_medium=rss&%3Butm_campaign=using-firebase-remote-config-in-android
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 .

Remote Config

Step 2 : After successful creation of your first project Click on Add Firebase to your Android app.

Remote Config

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

Remote Config

Step 4 : Add rules to your root-level build.gradle file

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

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.

Remote Config

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.

Remote Config

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

<?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()

FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
firebaseRemoteConfig.setDefaults(R.xml.default_config);

Step 10 : Fetch the value of background_color key by using firebaseRemoteConfig object

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.

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

He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.

Ravi Rupareliya January 28, 2017

firebase / remote config


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK