Firebase Android: Remote Config

Tram Ho

In my introductory article, I briefly mentioned about Remote Config. You can use Firebase Remote Config to define parameters in your application and update their values ​​in the cloud, allowing you to modify the application’s interface and behavior without having to issue an update. . Today I will guide you through how to implement it! Let’s go!

1. Add Firebase and Remote Config SDK to the app

  1. If you have not configured Firebase with the app, please follow the steps here !
  2. For Remote Config, Google Analytics is required for adding rules when configuring remote. Can you check if Google Analytics is enabled in the project or not?
  3. Add the dependences for the Remote Config Android library to the build.gradle file (app-level):

2. Retrieve Remote Config object

Get the Remote Config object, then set the minimum time to fetch data:

During the development process, you should set this fetch time a little low, the reason is in the Throttling section! 😄

3. Set the default values ​​for parameters in the app

You can set the default values ​​for the parameters in the app, so that the application works by default before connecting to the Remote Config backend. And when the backend has no value returned, the app will still work with that default value.

  1. Define name-value for default parameters, using the Map object or an .xml file stored in the res / xml directory:

  2. Set these values ​​for the Remote Config object:

4. How to retrieve parameter values

You can now get the parameter values ​​from the Remote Config object. If you set those values ​​in the backend, it will be fetched and activated, then use at your discretion. The following methods are available to help you retrieve values:

  • getBoolean ()
  • getByteArray ()
  • getDouble ()
  • getLong ()
  • getString ()

5. How to set the backend parameter values

Using the Firebase console or the Remote Config REST API, you can create new values ​​for the Remote Config so that it overrides the values ​​in the default file. I instructed on the Firebase console:

  1. On the Firebase console, open your project.
  2. Select Remote Config from the menu on the left of the console screen then select Add parameter .
  3. Define the parameters you need to use in this, note that the parameter name must match the name you defined in the default file (remote_config_defaults.xml). For each parameter you can create default values ​​or conditional values.

6. Fetch and activate the values

  1. To fetch values ​​from the backend, use the fetch () function, the values ​​in the backend will be retrieved and stored in the Remote Config object.
  2. To use it, call the active () method In case you want to fetch and active in the same place, use the method * fetchAndActivate () *

Because these parameters affect the interface and user experience, you should fetch and activate the values ​​at the same time to make the app smoother.

7. Throttling

If your app fetches too many times in a short period of time, the fetch call will be moderated and the SDK will return FirebaseRemoteConfigFetchThrottledException. When the SDK version is <17.0.0, the limit is 5 requests per hour. In the process of development and testing, you want to fetch it even shorter, which can be adjusted by using FirebaseRemoteConfigSettings.setMinimumFetchIntervalInSeconds (long) with the desired low fetch time.

And by default, the request time will be 12 hours, the default time will be determined by the following priority:

  1. Parameters passed into the fetch (long) method
  2. Parameters in FirebaseRemoteConfigSettings.setMinimumFetchIntervalInSeconds (long)
  3. The default is 12 hours.

Remember that FirebaseRemoteConfigSettings.Builder.setMinimumFetchIntervalInSeconds (long) is only used for the develop version, not the product version.

My post here is over, wish everyone happy code! 😄

Reference: https://firebase.google.com/docs/remote-config/use-config-android

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo