FirebaseRemoteConfig getString returns empty but bytearray of remote config is not empty

☆樱花仙子☆ 提交于 2021-02-10 14:50:44

问题


At first image, I stopped application where I try to get some data from FirebaseRemoteConfig. The code I evaluated is a simple code to get string from FirebaseRemoteConfig. But it returns empty string.

Then in second image I go a bit deep inside of FirebaseRemoteConfig object, and find out that the hashmap of zzogi.zzogw.get("configns:firebase") has what I need.

Why remoteConfig.getString() does not return my value even though remoteConfig have the value?

FYI this is gradle lines

implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-config:11.8.0'

and this is the code that I create FirebaseRemoteConfig Object. If you need more code, please ask.

public FirebaseRemoteConfig getFirebaseRemoteConfig(){
    FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings settings = new FirebaseRemoteConfigSettings.Builder()
            .setDeveloperModeEnabled(true)
            .build();
    config.setConfigSettings(settings);
    return config;
}


回答1:


You should add default values for all your config parameters. You can create a xml resource with a name config at xml folder for default values like this.

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
        <key>foo</key>
        <value>bar</value>
    </entry>
    <entry>
        <key>some_key</key>
        <value>true</value>
    </entry>
    <entry>
        <key>another_key</key>
        <value>2</value>
    </entry>
</defaultsMap>

You do not need to set config and defaults every time when you need a FirebaseRemoteConfig instance. Trigger this only once in onCreate method of your main activity.

public void configureFirebaseRemoteConfigInstance(){
    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
            .setDeveloperModeEnabled(BuildConfig.DEBUG)
            .build();
    mFirebaseRemoteConfig.setConfigSettings(configSettings);
    mFirebaseRemoteConfig.setDefaults(R.xml.config);
}

And then fetch your overrides from server. You can trigger this at onResume. It wont check server until cacheExpiration timeouts. Also when you are in development mode cacheExpiration will be 0. No need to change code for production.

public void refreshConfigValuesIfNeeded() {
    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    long cacheExpiration = 7200; //2 hour in seconds.
    if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
        cacheExpiration = 0;
    }
    mFirebaseRemoteConfig.fetch(cacheExpiration).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            FirebaseRemoteConfig.getInstance().activateFetched();
        }
    });
}

After all, your can reach your config values where you need:

FirebaseRemoteConfig.getInstance().getString("foo"); //Outputs bar
FirebaseRemoteConfig.getInstance().getBoolean("some_key"); //Outputs true
FirebaseRemoteConfig.getInstance().getLong("another_key"); // Outputs 2


来源:https://stackoverflow.com/questions/52971807/firebaseremoteconfig-getstring-returns-empty-but-bytearray-of-remote-config-is-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!