Firebase 遠端配置入門

您可以使用 Firebase Remote Config 在應用程式中定義參數並在雲端中更新其值,從而無需分發應用程式更新即可修改應用程式的外觀和行為。


第 1 步:將 Firebase 新增至您的應用


  • 註冊您的 C++ 專案並將其配置為使用 Firebase。

    如果您的 C++ 專案已使用 Firebase,則它已針對 Firebase 進行註冊和設定。

  • Firebase C++ SDK加入到您的 C++ 專案。

請注意,將 Firebase 新增至 C++ 專案涉及Firebase 控制台和開啟的 C++ 專案中的任務(例如,從控制台下載 Firebase 設定文件,然後將它們移至 C++ 專案中)。

第 2 步:將遠端配置新增至您的應用程式


將 Firebase 新增到您的應用程式後:

  1. 建立 Firebase 應用程序,傳入 JNI 環境和 Activity:

    app = ::firebase::App::Create(::firebase::AppOptions(), jni_env, activity);

  2. 初始化Remote Config函式庫,如圖:



將 Firebase 新增到您的應用程式後:

  1. 建立 Firebase 應用程式:

    app = ::firebase::App::Create(::firebase::AppOptions());

  2. 初始化Remote Config函式庫,如圖:


步驟 3:設定應用程式內預設參數值


  1. 使用std::map<const char*, const char*>物件或std::map<const char*, firebase::Variant>物件定義一組參數名稱和預設參數值。


  2. 使用SetDefaults()將這些值新增至遠端配置物件。

第 4 步:取得要在應用程式中使用的參數值



步驟 5:在 Firebase 控制台中連接您的應用

Firebase 控制台中,將您的應用程式新增至您的 Firebase 專案。


  1. Firebase 控制台中,開啟您的專案。
  2. 從選單中選擇遠端配置以查看遠端配置儀表板。
  3. 定義與您在應用程式中定義的參數同名的參數。 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

  1. 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.
  2. 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.

  1. 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, when Activate is called, uses the newly fetched values to display an updated welcome message.
    [](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(),
                    "welcome_message") != config_update.updated_keys.end()) {
            [&](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) {
              } else {
                printf("Error activating config: %d", completed_future.error());

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: