開始使用 C++ 的遠端設定

選取平台: iOS+ Android Web Flutter Unity C++


您可以使用 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:在應用程式中加入 Remote Config

Android

將 Firebase 新增至應用程式後:

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

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

  2. 初始化 Remote Config 程式庫,如下所示:

    ::firebase::remote_config::Initialize(app);

iOS+

將 Firebase 新增至應用程式後:

  1. 建立 Firebase 應用程式:

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

  2. 初始化 Remote Config 程式庫,如下所示:

    ::firebase::remote_config::Initialize(app);

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

您可以在 Remote Config 物件中設定應用程式內預設參數值,確保應用程式在連線至 Remote Config 後端前能正常運作,並在後端未設定任何值時提供預設值。

  1. 使用 ConfigKeyValue* 物件或 ConfigKeyValueVariant* 物件,定義一組參數名稱和預設參數值,並指定陣列大小。

    如果您已設定 Remote Config 後端參數值,可以下載包含這些鍵/值組合的檔案,並用來建構 map 物件。詳情請參閱「下載Remote Config範本預設值」。

  2. 使用 SetDefaults() 將這些值新增至 Remote Config 物件。

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

現在,您可以從 Remote Config 物件取得參數值。如果您在 Remote Config 後端設定值、擷取並啟用這些值,應用程式就能使用這些值。否則,您會取得使用 SetDefaults() 設定的應用程式內參數值。

如要取得這些值,請呼叫下列任一方法,對應至應用程式預期的資料類型,並提供參數鍵做為引數:

步驟 5:設定參數值

  1. 在 Firebase 控制台中,依序前往「DevOps & 參與」> 「遠端設定」。

  2. 定義參數時,請使用與應用程式中定義的參數相同的名稱。您可以為每個參數設定預設值 (最終會覆寫應用程式內預設值) 和條件值。詳情請參閱「Remote Config 參數和條件」。

步驟 6:擷取並啟用值

  1. 如要從 Remote Config 後端擷取參數值,請呼叫 Fetch() 方法。您在後端設定的任何值都會擷取並快取到 Remote Config 物件中。
  2. 如要讓應用程式使用擷取的參數值,請呼叫 ActivateFetched()

步驟 7:即時監聽更新

擷取參數值後,您可以使用即時 Remote Config 監聽 Remote Config 後端的更新。當有可用更新時,系統會即時向連結裝置發出 Remote Config 信號,並在您發布新的 Remote Config 版本後自動擷取變更。

Android 和 Apple 平台的 Firebase C++ SDK 11.0.0 以上版本支援即時更新。

  1. 在應用程式中呼叫 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);
      }
    });

下次發布新版 Remote Config 時,執行應用程式並監聽變更的裝置會呼叫設定更新監聽器。