Firebase is back at Google I/O on May 10! Register now

開始使用 Firebase 遠程配置

透過集合功能整理內容 你可以依據偏好儲存及分類內容。


您可以使用 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 添加到您的應用後:

  1. 創建一個Firebase App,傳入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. 使用std::map<const char*, const char*>對像或std::map<const char*, firebase::Variant>對象定義一組參數名稱和默認參數值。

    如果您已經配置了 Remote Config 後端參數值,您可以下載包含這些鍵/值對的文件並使用它來構建您的map對象。有關詳細信息,請參閱下載遠程配置模板默認值

  2. 使用SetDefaults()將這些值添加到遠程配置對象。

第 4 步:獲取要在您的應用中使用的參數值

現在您可以從 Remote Config 對像中獲取參數值。如果您在 Remote Config 後端設置值,獲取它們,然後激活它們,則這些值可用於您的應用程序。否則,您將獲得使用SetDefaults()配置的應用內參數值。

要獲取這些值,請調用下面列出的映射到您的應用程序期望的數據類型的方法,並提供參數鍵作為參數:

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

Firebase 控制台中,將您的應用程序添加到您的 Firebase 項目。

第 6 步:設置參數值

  1. Firebase 控制台中,打開您的項目。
  2. 從菜單中選擇遠程配置以查看遠程配置儀表板。
  3. 使用與您在應用程序中定義的參數相同的名稱定義參數。對於每個參數,您可以設置默認值(最終將覆蓋應用內默認值)和條件值。要了解更多信息,請參閱遠程配置參數和條件

第 7 步:獲取並激活值

  1. 要從遠程配置後端獲取參數值,請調用Fetch()方法。您在後端設置的任何值都將被提取並緩存在遠程配置對像中。
  2. 要使獲取的參數值可用於您的應用程序,請調用ActivateFetched()

下一步

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:

,


You can use Firebase Remote Config to define parameters in your app and update their values in the cloud, allowing you to modify the appearance and behavior of your app without distributing an app update.

The Remote Config library is used to store in-app default parameter values, fetch updated parameter values from the Remote Config backend, and control when fetched values are made available to your app. To learn more, see Remote Config loading strategies .

Step 1: Add Firebase to your app

Before you can use Remote Config , you need to:

  • Register your C++ project and configure it to use Firebase.

    If your C++ project already uses Firebase, then it's already registered and configured for Firebase.

  • Add the Firebase C++ SDK to your C++ project.

Note that adding Firebase to your C++ project involves tasks both in the Firebase console and in your open C++ project (for example, you download Firebase config files from the console, then move them into your C++ project).

Step 2: Add Remote Config to your app

Android

After you've added Firebase to your app:

  1. Create a Firebase App, passing in the JNI environment and Activity:

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

  2. Initialize the Remote Config library, as shown:

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

iOS+

After you've added Firebase to your app:

  1. Create a Firebase App:

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

  2. Initialize the Remote Config library, as shown:

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

Step 3: Set in-app default parameter values

You can set in-app default parameter values in the Remote Config object, so that your app behaves as intended before it connects to the Remote Config backend, and so that default values are available if none are set on the backend.

  1. Define a set of parameter names, and default parameter values using a std::map<const char*, const char*> object or a std::map<const char*, firebase::Variant> object.

    If you have already configured Remote Config backend parameter values, you can download a file that contains these key/value pairs and use it to construct your map object. For more information, see Download Remote Config template defaults .

  2. Add these values to the Remote Config object using SetDefaults() .

Step 4: Get parameter values to use in your app

Now you can get parameter values from the Remote Config object. If you set values in the Remote Config backend, fetched them, and then activated them, those values are available to your app. Otherwise, you get the in-app parameter values configured using SetDefaults() .

To get these values, call the method listed below that maps to the data type expected by your app, providing the parameter key as an argument:

Step 5: Connect your app in the Firebase console

In the Firebase console , add your app to your Firebase project.

Step 6: Set parameter values

  1. In the Firebase console , open your project.
  2. Select Remote Config from the menu to view the Remote Config dashboard.
  3. Define parameters with the same names as the parameters that you defined in your app. 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()

下一步

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: