Firebase远程配置fetch()根本不会触发

时间:2016-10-15 11:17:06

标签: android firebase firebase-remote-config

我正在制作一个演示项目,使用Firebase远程配置更改简单TextView的颜色。但函数mRemoteConfig.fetch(cacheExpiration).addOnCompleteListener ...根本不会触发。文字颜色不会改变。

这是我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text);
    initRemoteConfig();
}


private void initRemoteConfig() {
    mRemoteConfig = FirebaseRemoteConfig.getInstance();

    HashMap<String, Object> defaults = new HashMap<>();
    defaults.put("color_primary", getString(R.string.color_primary));

    mRemoteConfig.setDefaults(defaults);
    FirebaseRemoteConfigSettings remoteConfigSettings = new FirebaseRemoteConfigSettings.Builder()
            .setDeveloperModeEnabled(true)
            .build();
    mRemoteConfig.setConfigSettings(remoteConfigSettings);
    fetchRemoteConfigValues();
}

private void fetchRemoteConfigValues() {
    long cacheExpiration = 3600;

    //expire the cache immediately for development mode.
    if (mRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
        cacheExpiration = 0;
    }

    mRemoteConfig.fetch(cacheExpiration)
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(Task<Void> task) {
                    if (task.isSuccessful()) {
                        // task successful. Activate the fetched data
                        mRemoteConfig.activateFetched();
                        setupView();
                    } else {
                        //task failed
                    }
                }
            });
}

private void setupView() {
    setTextColor();
}

private void setTextColor() {
    boolean isPromoOn = true;
    int color = isPromoOn ? Color.parseColor(mRemoteConfig.getString("color_primary")) :
            ContextCompat.getColor(this, R.color.color_primary);

    text.setTextColor(color);
}

我错过了什么吗?感谢您的帮助:)

1 个答案:

答案 0 :(得分:-1)

如果您已正确设置Firebase控制台,我建议您检查手机上是否有最新的 Google Play服务