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

前端 未结 10 867
长发绾君心
长发绾君心 2020-12-30 19:10

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:

I

相关标签:
10条回答
  • 2020-12-30 19:25

    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);
    
    0 讨论(0)
  • 2020-12-30 19:30

    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.

    0 讨论(0)
  • 2020-12-30 19:33

    Well in my case, I am able to receive control in addOnCompleteListener for fetch method but I have fetched values firebaseRemoteConfig just after I called firebaseRemoteConfig.activate(), so when I have tried to get the values from firebaseRemoteConfig it returns me previously saved values because firebaseRemoteConfig.activate() runs asynchronously and new values didn't saved before I am getting them from firebaseRemoteConfig, so I have added complete listener for activate() method also, Here:

    firebaseRemoteConfig.fetch()
                .addOnCompleteListener(activity, OnCompleteListener {
                    if (it.isSuccessful)
                    {
                        Log.d("task","success")
                        firebaseRemoteConfig.activate().addOnCompleteListener {  // here I have added a listener
                            val base_url=firebaseRemoteConfig.getString("base_url")
                            Log.d("base url",base_url)
                            Toast.makeText(activity, "Base url: $base_url",Toast.LENGTH_SHORT).show()
                        }
                    }
                    else
                    {
                        Log.d("task","failure")
                    }
    
                })
    
    0 讨论(0)
  • 2020-12-30 19:33

    I work on a big project and the problem was buried in an unexpected place. Long story short: the firebase application id(normally set through google-services.json) was changed through code:

    FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
    builder.setApplicationId(applicationId);
    builder.setApiKey(apiKey);
    FirebaseOptions options = builder.build();
    FirebaseApp.initializeApp(context, options);
    

    The solution was to remove that code and let firebase use the info from "google-services.json".

    0 讨论(0)
  • 2020-12-30 19:34

    I had a problem that Firebase Remote Config didn't fire OnCompleteListener with fetch(0), but with fetch() did.

    Looking at FirebaseRemoteConfig.fetch() does not trigger OnCompleteListener every time, I found that the first answer was working sometimes even with fetch(0). Then I again set 3600 seconds for interval, as errors continued to appear:

    override fun onPostResume() {
        super.onPostResume()
    
        // Initialize FirebaseRemoteConfig here.
        ...
    
        firebaseRemoteConfig.fetch(3600).addOnCompleteListener { task ->
            if (task.isSuccessful) {
                firebaseRemoteConfig.activateFetched()
                //calling function to check if new version is available or not
                checkForUpdate(currentVersionCode, firebaseRemoteConfig.getString(VERSION_CODE_KEY))
            } else
                Toast.makeText(this@MainActivity, "Someting went wrong please try again",
                    Toast.LENGTH_SHORT).show()
        }
    
    }
    
    0 讨论(0)
  • 2020-12-30 19:36

    I've used a similar code like @Ian Barber (copy):

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

    My problem was the "BuildConfig.DEBUG", it returns false. So it takes the value 1h in cache until it was fetched again!

    0 讨论(0)
提交回复
热议问题