前往主控台

Firebase Remote Config Android Sample App Walkthrough

This guide walks you through the steps involved in integrating Remote Config into your Android app. To learn more about the Remote Config API for Android, see com.google.firebase.remoteconfig.

Requirements

Set up your project using the instructions provided in Adding Firebase to your Android project.

Next, add the following dependency to your app module's build.gradle:

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

Create a Remote Config project for the quickstart sample

The quickstart sample provides an example of using Remote Config to define the welcome message of the sample app. Before running the quickstart sample, you should set up a Remote Config project.

In the Firebase console, click Create New Project, and then follow the instructions to Set up a Firebase Remote Config Project with the following parameters:

Parameter key Default value Notes:
welcome_message Welcome to this sample app Change to use a different welcome message.
welcome_message_caps false Set to true to have the welcome message display in all caps.

After configuring your project, run the quickstart sample to see the welcome message you defined in the Firebase console. You can change the values of these parameters in the project and then tap Fetch Remote Config in the quickstart sample to see how Remote Config lets you update an app.

Build and run the quickstart sample

  1. Download the quickstart sample code and unzip it.
  2. Open Android Studio.
  3. Select File > Open, browse to where you extracted the sample code, and then open the Config folder.
  4. Build and run the sample.

How it Works

First, the sample gets a Remote Config object instance and sets the minimum fetch interval to allow for frequent refreshes:

Java

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setDeveloperModeEnabled(BuildConfig.DEBUG)
        .setMinimumFetchIntervalInSeconds(3600)
        .build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);

Kotlin

remoteConfig = FirebaseRemoteConfig.getInstance()
val configSettings = FirebaseRemoteConfigSettings.Builder()
        .setDeveloperModeEnabled(BuildConfig.DEBUG)
        .setMinimumFetchIntervalInSeconds(4200)
        .build()
remoteConfig.setConfigSettings(configSettings)

Then, the sample sets in-app default values from an XML file:

Java

mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);

Kotlin

remoteConfig.setDefaults(R.xml.remote_config_defaults)

Now, the sample creates a fetchAndActivate() request to fetch values from the Remote Config backend and make them available to the app:

Java

mFirebaseRemoteConfig.fetchAndActivate()
        .addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
            @Override
            public void onComplete(@NonNull Task<Boolean> task) {
                if (task.isSuccessful()) {
                    boolean updated = task.getResult();
                    Log.d(TAG, "Config params updated: " + updated);
                    Toast.makeText(MainActivity.this, "Fetch and activate succeeded",
                            Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "Fetch failed",
                            Toast.LENGTH_SHORT).show();
                }
                displayWelcomeMessage();
            }
        });

Kotlin

remoteConfig.fetchAndActivate()
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                val updated = task.getResult()
                Log.d(TAG, "Config params updated: $updated")
                Toast.makeText(this, "Fetch and activate succeeded",
                        Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "Fetch failed",
                        Toast.LENGTH_SHORT).show()
            }
            displayWelcomeMessage()
        }

You can access your config values by calling one of the available get<type> methods (for example, getLong) on the FirebaseRemoteConfig object passed to the callback.

Now that the welcome message has been updated, you can display the updated welcome message in the app:

Java

private void displayWelcomeMessage() {
    String welcomeMessage = mFirebaseRemoteConfig.getString(WELCOME_MESSAGE_KEY);
    if (mFirebaseRemoteConfig.getBoolean(WELCOME_MESSAGE_CAPS_KEY)) {
        mWelcomeTextView.setAllCaps(true);
    } else {
        mWelcomeTextView.setAllCaps(false);
    }
    mWelcomeTextView.setText(welcomeMessage);
}

Kotlin

private fun displayWelcomeMessage() {
    val welcomeMessage = remoteConfig.getString(WELCOME_MESSAGE_KEY)
    welcomeTextView.isAllCaps = remoteConfig.getBoolean(WELCOME_MESSAGE_CAPS_KEY)
    welcomeTextView.text = welcomeMessage
}

companion object {

    private const val TAG = "MainActivity"

    // Remote Config keys
    private const val LOADING_PHRASE_CONFIG_KEY = "loading_phrase"
    private const val WELCOME_MESSAGE_KEY = "welcome_message"
    private const val WELCOME_MESSAGE_CAPS_KEY = "welcome_message_caps"
}

Throttling

If an app fetches too many times in a short time period, fetch calls may be throttled. In such cases, the SDK returns FirebaseRemoteConfigFetchThrottledException.

During app development, you might want to fetch and activate configs 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 with up to 10 developers, you can temporarily set a FirebaseRemoteConfigSettings object with a low minimum fetch interval (setMinimumFetchIntervalInSeconds) in your app.

The default minimum fetch interval for Remote Config is 12 hours, which means that configs won't be fetched from the backend more than once in a 12 hour window, regardless of how many fetch calls are actually made. Specifically, the minimum fetch interval is determined in this following order:

  1. The parameter in fetch(long)
  2. The parameter in FirebaseRemoteConfigSettings.setMinimumFetchIntervalInSeconds(long)
  3. The default value of 12 hours

To set the minimum fetch interval to a custom value, use FirebaseRemoteConfigSettings.Builder.setMinimumFetchIntervalInSeconds(long).

Next Steps

Once you have tried out the quickstart sample and have gained an understanding of how the API works, you can review advanced Remote Config topics like propagating Remote Config updates in real time and using the REST API. See these resources: