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

開始使用 Firebase 遠程配置

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


您可以使用 Firebase Remote Config 在您的應用程序中定義參數並在雲端更新它們的值,從而允許您修改應用程序的外觀和行為而無需分發應用程序更新。本指南將引導您完成入門步驟並提供一些示例代碼。

第 1 步:將 Firebase 和 Remote Config SDK 添加到您的應用

  1. 如果您還沒有安裝並初始化 Firebase SDKs for Flutter,請執行此操作。

  2. 對於 Remote Config,需要 Google Analytics 才能將應用程序實例有條件地定位到用戶屬性和受眾。確保在項目中啟用 Google Analytics

  3. 在 Flutter 項目的根目錄中,運行以下命令來安裝 Remote Config 插件:

    flutter pub add firebase_remote_config
    

    此外,作為設置遠程配置的一部分,您需要將適用於 Google Analytics 的 Firebase SDK 添加到您的應用中:

    flutter pub add firebase_analytics
    
  4. 重建你的項目:

    flutter run
    

第 2 步:獲取 Remote Config 單例對象

獲取遠程配置對象實例並設置最小獲取間隔以允許頻繁刷新:

final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
    fetchTimeout: const Duration(minutes: 1),
    minimumFetchInterval: const Duration(hours: 1),
));

單例對像用於存儲應用內默認參數值、從後端獲取更新的參數值,以及控制獲取的值何時可用於您的應用。

在開發過程中,建議設置一個相對較低的最小獲取間隔。有關詳細信息,請參閱節流

第 3 步:設置應用內默認參數值

您可以在 Remote Config 對像中設置應用內默認參數值,以便您的應用在連接到 Remote Config 後端之前按預期運行,並且如果沒有在後端設置默認值,則默認值可用。

await remoteConfig.setDefaults(const {
    "example_param_1": 42,
    "example_param_2": 3.14159,
    "example_param_3": true,
    "example_param_4": "Hello, world!",
});

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

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

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

  • getBool()
  • getDouble()
  • getInt()
  • getString()

第 5 步:在 Remote Config 後端設置參數值

使用 Firebase 控制台或遠程配置後端 API ,您可以創建新的服務器端默認值,根據您所需的條件邏輯或用戶定位覆蓋應用內值。本節介紹創建這些值的 Firebase 控制台步驟。

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

第 6 步:獲取並激活值

  1. 要從遠程配置後端獲取參數值,請調用fetch()方法。您在後端設置的任何值都將被提取並存儲在遠程配置對像中。

  2. 要使獲取的參數值對您的應用程序可用,請調用activate()方法。

    對於您希望在一次調用中獲取和激活值的情況,您可以使用fetchAndActivate()請求從遠程配置後端獲取值並將它們提供給應用程序:

    await remoteConfig.fetchAndActivate();
    

由於這些更新的參數值會影響應用的行為和外觀,因此您應該在確保用戶流暢體驗的時間激活獲取的值,例如用戶下次打開您的應用時。有關更多信息和示例,請參閱遠程配置加載策略

節流

如果一個應用程序在短時間內獲取太多次,獲取調用將被限制並且FirebaseRemoteConfiglastFetchStatus屬性的值將為RemoteConfigFetchStatus.throttle

Remote Config 的默認最小提取間隔為 12 小時,這意味著在 12 小時的窗口中不會從後端提取配置超過一次,無論實際進行了多少次提取調用。

在應用程序開發期間,您可能希望非常頻繁地(每小時多次)獲取和激活配置,以便在開發和測試應用程序時快速迭代。為了適應最多 10 名開發人員的項目的快速迭代,您可以使用setConfigSettings()臨時設置較低的最小提取間隔。

final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
    fetchTimeout: const Duration(minutes: 1),
    minimumFetchInterval: const Duration(minutes: 5),
));

下一步

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. This guide walks you through the steps to get started and provides some sample code.

Step 1: Add Firebase and the Remote Config SDK to your app

  1. Install and initialize the Firebase SDKs for Flutter if you haven't already done so.

  2. For Remote Config, Google Analytics is required for the conditional targeting of app instances to user properties and audiences. Make sure that you enable Google Analytics in your project.

  3. From the root directory of your Flutter project, run the following command to install the Remote Config plugin:

    flutter pub add firebase_remote_config
    

    Also, as part of setting up Remote Config, you need to add the Firebase SDK for Google Analytics to your app:

    flutter pub add firebase_analytics
    
  4. Rebuild your project:

    flutter run
    

Step 2: Get the Remote Config singleton object

Get a Remote Config object instance and set the minimum fetch interval to allow for frequent refreshes:

final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
    fetchTimeout: const Duration(minutes: 1),
    minimumFetchInterval: const Duration(hours: 1),
));

The singleton object is used to store in-app default parameter values, fetch updated parameter values from the backend, and control when fetched values are made available to your app.

During development, it's recommended to set a relatively low minimum fetch interval. See Throttling for more information.

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 in the backend.

await remoteConfig.setDefaults(const {
    "example_param_1": 42,
    "example_param_2": 3.14159,
    "example_param_3": true,
    "example_param_4": "Hello, world!",
});

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 backend, fetch them, and then activate 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:

  • getBool()
  • getDouble()
  • getInt()
  • getString()

Step 5: Set parameter values in the Remote Config backend

Using the Firebase console or the Remote Config backend APIs , you can create new server-side default values that override the in-app values according to your desired conditional logic or user targeting. This section describes the Firebase console steps to create these 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 corresponding in-app default value), and you can also set conditional values. To learn more, see Remote Config Parameters and Conditions .

Step 6: Fetch and activate values

  1. To fetch parameter values from the Remote Config backend, call the fetch() method. Any values that you set in the backend are fetched and stored in the Remote Config object.

  2. To make fetched parameter values available to your app, call the activate() method.

    For cases where you want to fetch and activate values in one call, you can use a fetchAndActivate() request to fetch values from the Remote Config backend and make them available to the app:

    await remoteConfig.fetchAndActivate();
    

Because these updated parameter values affect the behavior and appearance of your app, you should activate the fetched values at a time that ensures a smooth experience for your user, such as the next time that the user opens your app. See Remote Config loading strategies for more information and examples.

Throttling

If an app fetches too many times in a short time period, fetch calls will be throttled and the value of FirebaseRemoteConfig 's lastFetchStatus property will be RemoteConfigFetchStatus.throttle .

The default minimum fetch interval for Remote Config is 12 hours, which means that configs won't be fetched from the backend more than once in a 12 hour window, regardless of how many fetch calls are actually made.

During app development, you might want to fetch and activate configs very frequently (many times per hour) to let you rapidly iterate as you develop and test your app. To accommodate rapid iteration on a project with up to 10 developers, you can temporarily set a low minimum fetch interval with setConfigSettings() .

final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
    fetchTimeout: const Duration(minutes: 1),
    minimumFetchInterval: const Duration(minutes: 5),
));

下一步

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: