您可以使用 Firebase Remote Config 在應用程式中定義參數並在雲端中更新其值,從而無需分發應用程式更新即可修改應用程式的外觀和行為。
遠端配置庫用於儲存應用程式內預設參數值、從遠端配置後端取得更新的參數值,以及控制何時將取得的值提供給您的應用程式。要了解更多信息,請參閱遠端配置載入策略。
第 1 步:將 Firebase 新增至您的應用
在使用遠端配置之前,您需要:
註冊您的 C++ 專案並將其配置為使用 Firebase。
如果您的 C++ 專案已使用 Firebase,則它已針對 Firebase 進行註冊和設定。
將Firebase C++ SDK加入到您的 C++ 專案。
請注意,將 Firebase 新增至 C++ 專案涉及Firebase 控制台和開啟的 C++ 專案中的任務(例如,從控制台下載 Firebase 設定文件,然後將它們移至 C++ 專案中)。
第 2 步:將遠端配置新增至您的應用程式
安卓
將 Firebase 新增到您的應用程式後:
建立 Firebase 應用程序,傳入 JNI 環境和 Activity:
app = ::firebase::App::Create(::firebase::AppOptions(), jni_env, activity);
初始化Remote Config函式庫,如圖:
::firebase::remote_config::Initialize(app);
iOS+
將 Firebase 新增到您的應用程式後:
建立 Firebase 應用程式:
app = ::firebase::App::Create(::firebase::AppOptions());
初始化Remote Config函式庫,如圖:
::firebase::remote_config::Initialize(app);
步驟 3:設定應用程式內預設參數值
您可以在遠端配置物件中設定應用程式內預設參數值,以便您的應用程式在連接到遠端配置後端之前按預期運行,並且如果後端未設定預設值,則預設值可用。
使用
std::map<const char*, const char*>
物件或std::map<const char*, firebase::Variant>
物件定義一組參數名稱和預設參數值。如果您已經配置了遠端配置後端參數值,則可以下載包含這些鍵/值對的檔案並使用它來建構
map
物件。有關更多信息,請參閱下載遠端配置範本預設值。使用
SetDefaults()
將這些值新增至遠端配置物件。
第 4 步:取得要在應用程式中使用的參數值
現在您可以從遠端配置物件取得參數值。如果您在遠端配置後端設定值,取得它們,然後啟動它們,則這些值可供您的應用程式使用。否則,您將會取得使用SetDefaults()
配置的應用內參數值。
若要取得這些值,請呼叫下面列出的映射到應用程式所需的資料類型的方法,並提供參數鍵作為參數:
步驟 5:在 Firebase 控制台中連接您的應用
在Firebase 控制台中,將您的應用程式新增至您的 Firebase 專案。
第6步:設定參數值
- 在Firebase 控制台中,開啟您的專案。
- 從選單中選擇遠端配置以查看遠端配置儀表板。
- 定義與您在應用程式中定義的參數同名的參數。 For each parameter, you can set a default value (which will eventually override the in-app default value) and conditional values. To learn more, see Remote Config parameters and conditions .
Step 7: Fetch and activate values
- To fetch parameter values from the Remote Config backend, call the
Fetch()
method. Any values that you set on the backend are fetched and cached in the Remote Config object. - To make fetched parameter values available to your app, call the
ActivateFetched()
Step 8: Listen for updates in real time
After you fetch parameter values, you can use real-time Remote Config to listen for updates from the Remote Config backend. Real-time Remote Config signals to connected devices when updates are available and automatically fetches the changes after you publish a new Remote Config version.
Real-time updates are supported by the Firebase C++ SDK v11.0.0+ and higher for Android and Apple platforms.
- In your app, call
AddOnConfigUpdateListener
to start listening for updates and automatically fetch any new or updated parameter values. The following example listens for updates and, whenActivate
is called, uses the newly fetched values to display an updated welcome message.
remote_config->AddOnConfigUpdateListener( [](firebase::remote_config::ConfigUpdate&& config_update, firebase::remote_config::RemoteConfigError remote_config_error) { if (remote_config_error != firebase::remote_config::kRemoteConfigErrorNone) { printf("Error listening for config updates: %d", remote_config_error); } // Search the `updated_keys` set for the key "welcome_message." // `updated_keys` represents the keys that have changed since the last // fetch. if (std::find(config_update.updated_keys.begin(), config_update.updated_keys.end(), "welcome_message") != config_update.updated_keys.end()) { remote_config->Activate().OnCompletion( [&](const firebase::Future& completed_future, void* user_data) { // The key "welcome_message" was found within `updated_keys` and // can be activated. if (completed_future.error() == 0) { DisplayWelcomeMessage(); } else { printf("Error activating config: %d", completed_future.error()); } }, nullptr); } });
The next time you publish a new version of your Remote Config, devices that are running your app and listening for changes will call the config update listener.
Next steps
If you haven't already, explore the Remote Config use cases , and take a look at some of the key concepts and advanced strategies documentation, including: