0

我想要做的是在 Firebase 的 RemoteConfig 中保存 colorPrimay、colorDarkPrimary 等值,这样我就可以从远程配置中更改应用程序的颜色,我做了以下

更新:我要做的是在 Firebase 的 RemoteConfig 中保存 colorPrimay、colorDarkPrimary 等值,以便我可以从远程配置更改应用程序的颜色。我怎么能那样做?

更新:

我尝试过的就像

<--!remote_config_default.xml!-->


<defaultsMap>
<entry>
    <key>primaryColor</key>
    <value>#9c27b0</value>
</entry>
<entry>
    <key>colorPrimaryDark</key>
    <value>#7b1fa2</value>
</entry>
<entry>
    <key>colorAccent</key>
    <value>#FF4081</value>
</entry>
<entry>welcomeMessage</entry>
<value>Connection Failed</value>

像这样取

    void applyRemoteConfig() {
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_default);
        // cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously
// fetched and cached config would be considered expired because it would have been fetched
// more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless
// throttling is in progress. The default expiration duration is 43200 (12 hours).
        int cacheExpiration = 1000;
        final String TAG = "Riddles";
        mFirebaseRemoteConfig.fetch(cacheExpiration)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "Fetch Succeeded");
                            // Once the config is successfully fetched it must be activated before newly fetched
                            // values are returned.
                            mFirebaseRemoteConfig.activateFetched();
                        } else {
                            Log.d(TAG, "Fetch failed");
                            String message = mFirebaseRemoteConfig.getString("welcomeMessage");
                            displayWelcomeMessage(message);
                            Log.d(TAG, message);
                        }
                    }
                });

        String message = mFirebaseRemoteConfig.getString("welcomeMessage");
        displayWelcomeMessage(message);
        Log.d(TAG, message);


    }

但是

mFirebaseRemoteConfig.getString("colorPrimary");

mFirebaseRemoteConfig.getString("colorDarkPrimary");

mFirebaseRemoteConfig.getString("welcomeMessage");

正在返回 Null。

4

1 回答 1

2

您可以将其添加到您的远程配置文件中。

<!-- color entries -->
    <entry>
        <key>color_primary</key>
        <value>#3F51B5</value>
    </entry>
    <entry>
        <key>color_primary_dark</key>
        <value>#303F9F</value>
    </entry>  

要动态更改应用程序的颜色,请同时使用远程配置提供的标志并使用fetch(). 有关详细的实现,请参阅this。

于 2016-09-20T12:57:10.713 回答