FirebaseRemoteConfig fetchAndActivate not updating new value
Asked Answered
I

7

11

I have already set the minimum interval to be 0 when in Debug mode. Currently what I do to fetch new data is by clearing the app storage first before launching the app.

Here is my code:

private fun initRemoteConfig() {
    remoteConfig = FirebaseRemoteConfig.getInstance()
    configSettings = FirebaseRemoteConfigSettings.Builder()
        .setMinimumFetchIntervalInSeconds(if (BuildConfig.DEBUG) 0 else 3600)
        .build()
    remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
    fetch()
}

private fun fetch() {
    remoteConfig.fetchAndActivate()
        .addOnCompleteListener {
            if (it.isSuccessful) {
                val updated = it.result
                Logger.d(TAG, "Config params updated: $updated. Fetch and activate succeeded") // updated = false

                checkVersion()
            } else {
                Logger.d(TAG, "Fetch failed")
            }
        }

}
Intermix answered 14/2, 2020 at 2:43 Comment(0)
F
4

Try this

private fun initRemoteConfig() {
  remoteConfig = FirebaseRemoteConfig.getInstance()
  configSettings = FirebaseRemoteConfigSettings.Builder()
    .setMinimumFetchIntervalInSeconds(if (BuildConfig.DEBUG) 0 else 3600)
    .build()
  remoteConfig.setConfigSettingsAsync(configSettings)  //You missed this line
  remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
  fetch()
}

private fun fetch() {
  remoteConfig.fetchAndActivate()
    .addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            val updated = task.result
            Logger.d(TAG, "Config params updated: $updated. Fetch and activate succeeded") // updated = false
            checkVersion()
        } else {
            Logger.d(TAG, "Fetch failed")
        }
    }
}
Freighter answered 14/2, 2020 at 10:9 Comment(0)
A
3

I will try to shed some light on why replacing fetchAndActivate with fetch and activate works.

The reason is the caching of remote config behind the fetchAndActivate function. It caches the new data for an amount of time. That time defaults to -1 which possibly means forever (not sure about that though, could mean 1 day, or something along those lines).

When the data is already cached, it doesn't even try to fetch them again. Therefore it seems, as if it isn't working. When you call fetch(0) it sets caching time to 0 seconds.

That is the reason why it will fetch new remote config values every time.

Ardyce answered 18/2, 2021 at 11:40 Comment(1)
I don't think that's correct. calling fetch() with no arguments is equivalent to passing the minimumFetchIntervalInSeconds as the argument. You can check the code for that (the override is in class ConfigFetchHandler). I think @Freighter has the correct answer here, which is simply that OP missed to actually apply the configSettings object createdTravelled
I
2

I changed my code from fetchAndActivate to fetch and it seems to be working as expected. I am now getting new data from the Firebase.

Here is my updated code:

private fun fetch() {
    val fetch = remoteConfig.fetch(if (BuildConfig.DEBUG) 0 else TimeUnit.HOURS.toSeconds(12))
    fetch.addOnCompleteListener {
        if (it.isSuccessful) {
            remoteConfig.activate()
            Logger.d(TAG, "Fetch and activate succeeded")

            checkVersion()
        } else {
            Logger.d(TAG, "Fetch failed")
        }
    }
}
Intermix answered 14/2, 2020 at 8:0 Comment(0)
D
0

In the early stages of development, there is a method that can be used to test. It worked for me. It would be helpful if you refer to the code and Google Doc below.

For your information, you can estimate the time you have communicated with the server through 'remoteConfig.lastFetchTime'.

final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
    fetchTimeout: const Duration(minutes: 1),
    minimumFetchInterval: const Duration(minutes: 5),
));

During app development, if you're not using real-time Remote Config (which we recommend), 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 low minimum fetch interval with setConfigSettings().

link: https://firebase.google.com/docs/remote-config/get-started?platform=flutter#fetch-values

Diffraction answered 25/7, 2023 at 14:4 Comment(0)
S
0

The default remote update is 43200000 ms which are 720 minutes or 12 hours. It is recommended use the default.

If u wish to debug or use a new value u can set the

remoteConfig.settings.minimumFetchIntervalMillis = 5000

Debug your app and then remove it

Superfecundation answered 6/8, 2023 at 15:58 Comment(0)
A
0

In my case, I solved it calling the onConfigUpdated function before the fetchAndActivate, as the documentation suggests on step 7:

await remoteConfig.fetchAndActivate();

remoteConfig.onConfigUpdated.listen((event) async {
   print('### Remote Config Updated');
   await remoteConfig.activate();
});

Hope It helps someone :)

Ac answered 2/1 at 17:48 Comment(0)
P
0

for me fetching the values with passing time 0 first and then calling fetchAndActivate() solves the problem i am getting everytime updated config values now. here is the solution

async function getUpdate() {
    await remoteConfig().fetch(0);
    remoteConfig()
      .fetchAndActivate()
      .then(fetchedRemotely => {
        if (fetchedRemotely) {
          console.log('configs were fetched from the backend');
        } else {
          console.log(
            'No configs were fetched from the backend, and the local configs were already activated',
          );
        }
        const parameters = remoteConfig().getValue('pickup_app_version');
        console.log(parameters)
      });
  }
Poleyn answered 15/6 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.