Firebase Remote Config: Can't read any values, but fetch is successful

43,294

Solution 1

Found the problem.

After adding some logging, I found that the fetch job's onComplete() was never being called. I moved the fetch from my Application's onCreate to a fragment's, and now it works properly!

(Ian Barber, this might be something to look into or clarify, as the logs indicated that Firebase was initialized without an issue when it was in the Application, and the fetches were silent failures.)

Solution 2

Workaround found! See below

I'm running into the "silent completion" thing - I call "fetch" but onComplete, onSuccess, or onFailure listeners never fire. I tried moving it to an activity onCreate, and still nothing happened, and therefore, the config items never get loaded from the server. I've got Developer Mode enabled, and am calling fetch with a cache value of 0.

I was able to (once) put a breakpoint on the line "public void onComplete(@NonNull Task task) {", which got hit, and then I was able to step through and the onComplete fired. I was then unable to reproduce this same result any other way, including doing the same thing (I think) a second time.

Seems like a timing or concurrency issue, but that makes little sense, given this is an asynchronous call.

Workaround

If you fetch from Activity#onResume (or, I presume, Activity#onStart), it works perfectly. Calling fetch from Activity#onCreate or Application#onCreate results in a call that seemingly never gets handled, and in fact, performance of the app degrades noticeably after the fetch begins, so I think there's a looper running or something.*

Workaround #2

If you really want this to run from Application#onCreate (which I do), this seems to work as well:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // Run mFirebaseRemoteConfig.fetch(timeout) here, and it works
    }
}, 0);

Solution 3

You're likely hitting the caching in Remote Config. The way it works is that Config will cache incoming items locally, and return them. So your last (cached) fetch status was probably before the value was defined, and we get a cached blank value.

You can control the cache expiry, but if you fetch too often you risk getting throttled.

Because this is a common development problem though, there is a developer mode that lets you request more rapidly (for small groups of users):

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

When you call fetch you can then pass a short cache expiration time

long cacheExpiration = 3600;
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
     cacheExpiration = 0;
}
mFirebaseRemoteConfig.fetch(cacheExpiration)
     .addOnCompleteListener(new OnCompleteListener<Void>() {
     // ...
});

That's how its done in the quickstart sample if you want a full reference.

Solution 4

I also encountered this problem. Turns out I hadn't seen the 'Publish' button in the the Firebase console. :facepalm:

Solution 5

I had the same problem and no workarounds were helpful in my case. The problem was in the testing device. I used emulator without installing Google Mobile Services, because of this the Complete event was not fired. I tried my phone with GMS and everything worked great. Good luck.

Share:
43,294

Related videos on Youtube

Steven Schoen
Author by

Steven Schoen

Updated on July 09, 2022

Comments

  • Steven Schoen
    Steven Schoen almost 2 years

    I'm trying to have a remote config parameter using the new Remote Config feature of Firebase, and I'm having an issue.

    Here's my Remote Config console: remote config console

    I'm doing a fetch and update in my Application's onCreate():

    final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
    remoteConfig.fetch().addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                remoteConfig.activateFetched();
            }
        }
    });
    

    And here's how I'm reading it:

    FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
    String value = remoteConfig.getString("active_subscriptions");
    

    Value is returning null.

    If I call remoteConfig.getInfo().getLastFetchStatus(), it returns LAST_FETCH_STATUS_SUCCESS, so it seems the fetch is going through successfully.

    Any idea why my value is blank?

    • jkane001
      jkane001 about 8 years
      Steven, see my comment below - I tried your fix, and it didn't work for me, but I was able to make it work by moving the fetch out of onCreate.
    • Steven Elliott
      Steven Elliott about 8 years
      I'm having the same problem and moving the fetch out of onCreate doesn't work for me .. if I move it into onResume, it will work on a second on Resume but not the initial onResume. This isn't exactly ideal as I want to configure aspects of my app remotely from first launch, not "at some point later down the line" :(
    • CoolMind
      CoolMind over 5 years
  • Steven Schoen
    Steven Schoen about 8 years
    Unfortunately that didn't change anything.
  • Chirag
    Chirag about 8 years
    what if you need to use a fragment?
  • Steven Schoen
    Steven Schoen about 8 years
    I am using a fragment. Moving the fetch() call into the fragment fixed it for me.
  • Admin
    Admin about 8 years
    I had this issue as well and a handler (#2) got config fetch to work properly. Very strange.
  • Steven Elliott
    Steven Elliott about 8 years
    This seems to be a different issue. I can confirm using gFirebaseRemoteConfig.getInfo() that fetch had never returned, so it wasn't a caching issue
  • Ian Barber
    Ian Barber about 8 years
    That's interesting, we'll look into that. Thanks for the feedback!
  • jkane001
    jkane001 about 8 years
    Recently (I'm now using Firebase 9.0.1), the code I use in Workaround 2 isn't working for me anymore (and the bug still exists). So it looks like Workaround 1 is the only valid workaround now.
  • Alex
    Alex about 8 years
    fetching on onCreate() is okay and there's no need to put them in onStart(). The only things you have to do are enabling development mode and passing less seconds (i.e. 100) to the fetching method. It works very well. Thanks
  • Lancelot
    Lancelot about 8 years
    Hi all, I have a simple boolean parameter but I can't seem to get the right value from the server. I'm calling fetch from onCreate. I have two listeners one for success and one for failure. The success one gets called and then I use the activateFetched. But I still can't get the right value, it's coming as false when is set to true.
  • Lancelot
    Lancelot about 8 years
    Me again, keep testing and it's seems like Result inside the task response object is empty. It's like there is nothing to fetch but I have 3 properties now set LIVE on Firebase.
  • Lancelot
    Lancelot about 8 years
    It's now working, haven't changed anything just updated to the new version 9.0.2
  • SamIAmHarris
    SamIAmHarris about 8 years
    This fixed it for me. I am running mine in a DialogFragment. I tried moving it from onViewCreated to onResume, but it still didn't work. Once I added the Handler.post(Runnable), the fetch call started working correctly.
  • Lancelot
    Lancelot about 8 years
    And me again. Couple of weeks later, I removed old parameters and add new ones, and I'm not getting the new ones, just the old ones, more than 24h later.
  • android developer
    android developer about 8 years
    @IanBarber I have it in an activity, in onCreate, just like in the samples : github.com/firebase/quickstart-android/blob/master/config/ap‌​p/… , but for some reason, the function onComplete doesn't get called on most devices, while it seems ok on Nexus 5 with Android 6. How come? What can I do to fix it? I tried running it on both UI thread and background thread. Both didn't work well.
  • Lucas
    Lucas almost 8 years
    I was using this workaround since version 9.0.0. Version 9.4.0 solved this problem, it's working checking it out.
  • Thomas R.
    Thomas R. almost 8 years
    Version 9.4.0 does the trick for me too. If you still have the problem that the callback is not called, then check your device time. In my case the test device was 2 weeks in the past and therefore the fetching didn't work.
  • Rohit
    Rohit about 7 years
    in my case am using 10.2.4 and facing the same issue, code reached in onComplete but value is always null.
  • egorikem
    egorikem almost 6 years
    Just make sure that BuildConfig is actually from you app, not some other package
  • Sagar Shah
    Sagar Shah about 4 years
    Thanks a ton brother. Wasted my 1 hour before seeing this post.