10

I'm using Firebase Remote Config to fetch data when app first open for the first time. But problem is I cannot fetch data on first start of the app. onComplete() method triggers but returns no value. If I close app and run again it returns value from Remote Config.

Tried to call fetch() in onCreate(), onStart(), onResume(), gave it a second delay with postDelay() still the same, initial fetch is always empty. I know, Remote Config has setDefaults() method to store defaults before it is getting fetched, but setting defaults inside app is not what I want.

It is the way how Remote Config works or am I doing something wrong? The only workaround I found is to add fetch() inside onResume() and call onResume() again inside onCreate(). It results calling onResume() twice. First time by Android system and second time by code.

It there any other way to force Remote Config fetch data on first time run?

UPDATE

Inside onComplete() first I must call firebaseRemoteConfig.activateFetched(); before getting new values from it.

Orkhan Ahmadov
  • 103
  • 2
  • 7
  • 1
    In the `onComplete()` method of the fetch listener, is `task.isSuccessful()` true when fetch returns no values? If not, what is `task.getException().getMessage()`? – Bob Snyder Aug 11 '16 at 14:25
  • 1
    Found the issue. `activateFetched()` must be called before getting new values. Yet another silly mistake... – Orkhan Ahmadov Aug 11 '16 at 14:28
  • `activateFetched()` is deprecated, see https://stackoverflow.com/questions/57494893/firebaseremoteconfig-activatefetched-is-deprecated-what-to-use-instead. Anyway, I used the solution of Mohammad Alotol. – CoolMind Apr 02 '20 at 14:17

5 Answers5

5
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().build();
        mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings)
                .addOnCompleteListener(task -> mFirebaseRemoteConfig.fetchAndActivate()
                        .addOnCompleteListener(activity, task1 {
                       // now cache is updated you can fetch values
                       String value = mFirebaseRemoteConfig.getString("key");
               }));

Start getting the values after config settings are completed

just do that at the app launch, and get values directly anywhere else (eg: mFirebaseRemoteConfig.getString("key");)

Mohammad Alotol
  • 1,409
  • 15
  • 23
  • I'd also like to add that "OnSuccessListener" and "OnCompleteListener" are not the same thing! OnSuccessListener was not giving me the latest data on it's first fetch, but OnCompleteListener did. – ConcernedHobbit Mar 29 '21 at 15:02
2

check few points:

Once new variable is created in firebase dashboard, publish it

fetch with cache time set to 0

FirebaseRemoteConfig.getInstance().fetch(0).
Pankaj kumar
  • 1,357
  • 14
  • 13
1

Simply just try to reset your config locally before getting it to prevent wrong values from getting back.

 val firebaseRemoteConfig = Firebase.remoteConfig

 firebaseRemoteConfig.reset()

 firebaseRemoteConfig.fetch(0)
    .addOnCompleteListener { taskFetch ->
        if (taskFetch.isSuccessful) {
            firebaseRemoteConfig.activate().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                 //do what you want..
                }
            }
        }
    }

Just the point is firebaseRemoteConfig.reset() before the activation is called.

TinaTT2
  • 366
  • 5
  • 17
0

I understand that this is an old question - but I ran into exactly the same issue as the asker. After investigating this for a few days I was able to find an answer so I will post it here to help anyone else who is having difficulties.

The first thing to check is that your config settings are published in Firebase. After making a change there is a Publish button on the top right of the console.

I had followed numerous tutorials and guides onto how configure FireBases Remote Config in Unity but despite calling ActivateFetched I got no results. After investing a great deal of effort trying to figure out why no data was being returned I stumbled across this thread: https://github.com/firebase/quickstart-unity/issues/9

It turns out RemoteConfig doesn't work in Unity and will only use the defaults. To actually using your remote config you must do a build and deploy it onto your target device.

On my Android device all of my remote config is retrieved successfully

user1662292
  • 137
  • 1
  • 2
  • 12
  • I am working with Unity and calling ActivateFetched immediately after start to retrieve previous config (as Strategy #3: Load values for next time). If this strategy is working correctly it will update value on the next start. But nothing changes. If I called ActivateFetched after Firebase Load complete, the value is updated correctly. I wonder if Unity Editor working with ActivateFetched or not? – hlongvu Mar 03 '20 at 04:42
0

Thanks to Pankaj kumar answer, I set firebase.remoteConfig().settings.minimumFetchIntervalMillis = 0 and worked

Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34