您可以使用 Firebase Remote Config 在您的應用程序中定義參數並在雲端更新它們的值,從而允許您修改應用程序的外觀和行為而無需分發應用程序更新。
Remote Config 庫用於存儲應用內默認參數值、從 Remote Config 後端獲取更新的參數值,以及控制獲取的值何時可用於您的應用。要了解更多信息,請參閱遠程配置加載策略。
第 1 步:將 Firebase 添加到您的應用
在使用Remote Config之前,您需要:
註冊您的 C++ 項目並將其配置為使用 Firebase。
如果您的 C++ 項目已經使用 Firebase,那麼它已經針對 Firebase 進行了註冊和配置。
將Firebase C++ SDK添加到您的 C++ 項目。
請注意,將 Firebase 添加到您的 C++ 項目涉及Firebase 控制台和您打開的 C++ 項目中的任務(例如,您從控制台下載 Firebase 配置文件,然後將它們移動到您的 C++ 項目中)。
第 2 步:將遠程配置添加到您的應用
安卓
在您將 Firebase 添加到您的應用後:
創建一個Firebase App,傳入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 步:設置應用內默認參數值
您可以在 Remote Config 對像中設置應用內默認參數值,以便您的應用在連接到 Remote Config 後端之前按預期運行,並且如果沒有在後端設置默認值,則默認值可用。
使用
std::map<const char*, const char*>
對像或std::map<const char*, firebase::Variant>
對象定義一組參數名稱和默認參數值。如果您已經配置了 Remote Config 後端參數值,您可以下載包含這些鍵/值對的文件並使用它來構建您的
map
對象。有關詳細信息,請參閱下載遠程配置模板默認值。使用
SetDefaults()
將這些值添加到遠程配置對象。
第 4 步:獲取要在您的應用中使用的參數值
現在您可以從 Remote Config 對像中獲取參數值。如果您在 Remote Config 後端設置值,獲取它們,然後激活它們,則這些值可用於您的應用程序。否則,您將獲得使用SetDefaults()
配置的應用內參數值。
要獲取這些值,請調用下面列出的方法,該方法映射到您的應用程序所需的數據類型,並提供參數鍵作為參數:
第 5 步:在 Firebase 控制台中連接您的應用
在Firebase 控制台中,將您的應用程序添加到您的 Firebase 項目。
第 6 步:設置參數值
- 在Firebase 控制台中,打開您的項目。
- 從菜單中選擇遠程配置以查看遠程配置儀表板。
- 使用與您在應用程序中定義的參數相同的名稱定義參數。對於每個參數,您可以設置默認值(最終將覆蓋應用內默認值)和條件值。要了解更多信息,請參閱遠程配置參數和條件。
第 7 步:獲取並激活值
- 要從遠程配置後端獲取參數值,請調用
Fetch()
方法。您在後端設置的任何值都將被提取並緩存在遠程配置對像中。 - 要使獲取的參數值可用於您的應用程序,請調用
ActivateFetched()
第 8 步:實時收聽更新
獲取參數值後,您可以使用實時 Remote Config 來監聽來自 Remote Config 後端的更新。實時遠程配置在更新可用時向連接的設備發出信號,並在您發布新的遠程配置版本後自動獲取更改。
Android 和 Apple 平台的 Firebase C++ SDK v11.0.0+ 及更高版本支持實時更新。
- 在您的應用程序中,調用
AddOnConfigUpdateListener
以開始偵聽更新並自動獲取任何新的或更新的參數值。以下示例偵聽更新,並在調用Activate
時使用新獲取的值來顯示更新的歡迎消息。
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); } });
下次您發布新版本的遠程配置時,運行您的應用並監聽更改的設備將調用配置更新監聽器。
下一步
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: