Catch up on highlights from Firebase at Google I/O 2023. Learn more

開始使用 Firebase 遠程配置


您可以使用 Firebase Remote Config 在您的應用程序中定義參數並在雲端更新它們的值,從而允許您修改應用程序的外觀和行為而無需分發應用程序更新。本指南將引導您完成入門步驟並提供一些示例代碼,所有這些代碼都可以從firebase/quickstart-js GitHub 存儲庫中克隆或下載。

第一步:添加並初始化Remote Config SDK

  1. 如果您還沒有安裝 Firebase JS SDK 並初始化 Firebase

  2. 添加 Remote Config JS SDK 並初始化 Remote Config:

網絡模塊化 API

import { initializeApp } from "firebase/app";
import { getRemoteConfig } from "firebase/remote-config";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);


// Initialize Remote Config and get a reference to the service
const remoteConfig = getRemoteConfig(app);

Web 命名空間 API

import firebase from "firebase/compat/app";
import "firebase/compat/remote-config";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);


// Initialize Remote Config and get a reference to the service
const remoteConfig = firebase.remoteConfig();

此對像用於存儲應用內默認參數值、從遠程配置後端獲取更新的參數值,以及控制何時將獲取的值提供給您的應用。

第 2 步:設置最小提取間隔

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

Web modular API

remoteConfig.settings.minimumFetchIntervalMillis = 3600000;

Web namespaced API

remoteConfig.settings.minimumFetchIntervalMillis = 3600000;

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

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

Web modular API

remoteConfig.defaultConfig = {
  "welcome_message": "Welcome"
};

Web namespaced API

remoteConfig.defaultConfig = {
  "welcome_message": "Welcome"
};

如果您已經配置了遠程配置後端參數值,則可以下載包含所有默認值的生成的 JSON 文件並將其包含在您的應用程序包中:

休息

curl --compressed -D headers -H "Authorization: Bearer token" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:downloadDefaults?format=JSON -o remote_config_defaults.json

Firebase 控制台

  1. Parameters選項卡中,打開 Menu ,然後選擇Download default values
  2. 出現提示時,啟用.json for web ,然後單擊Download file

以下示例展示了您可以在應用中導入和設置默認值的兩種不同方式。第一個示例使用fetch ,它將向您的應用程序包中包含的默認文件發出 HTTP 請求:


  const rcDefaultsFile = await fetch('remote_config_defaults.json');
  const rcDefaultsJson = await rcDefaultsFile.json();
  remoteConfig.defaultConfig = rcDefaultsJson;
  

下一個示例使用require ,它在構建時將值編譯到您的應用程序中:

  let rcDefaults = require('./remote_config_defaults.json');
  remoteConfig.defaultConfig = rcDefaults;

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

現在您可以從 Remote Config 對像中獲取參數值。如果您稍後在後端設置值,獲取它們,然後激活它們,這些值可用於您的應用程序。要獲取這些值,請調用getValue()方法,提供參數鍵作為參數。

Web modular API

import { getValue } from "firebase/remote-config";

const val = getValue(remoteConfig, "welcome_messsage");

Web namespaced API

const val = remoteConfig.getValue("welcome_messsage");

第 5 步:設置參數值

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

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

第 6 步:獲取並激活值

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

對於希望在一次調用中獲取和激活值的情況,請使用fetchAndActivate()如本例所示:

Web modular API

import { fetchAndActivate } from "firebase/remote-config";

fetchAndActivate(remoteConfig)
  .then(() => {
    // ...
  })
  .catch((err) => {
    // ...
  });

Web namespaced API

remoteConfig.fetchAndActivate()
  .then(() => {
    // ...
  })
  .catch((err) => {
    // ...
  });

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

節流

如果應用程序在短時間內獲取的次數太多,獲取調用可能會受到限制。在這種情況下,SDK 會拋出FETCH_THROTTLE錯誤。建議您捕獲此錯誤並以指數退避模式重試,在後續提取請求之間等待更長的間隔。

在應用程序開發期間,您可能希望非常頻繁地刷新緩存(每小時多次),以便在開發和測試應用程序時快速迭代。為了適應擁有眾多開發人員的項目的快速迭代,您可以在您的應用中臨時添加一個具有較低最小獲取間隔 ( Settings.minimumFetchIntervalMillis ) 的屬性。

遠程配置的默認和推薦生產獲取間隔為 12 小時,這意味著在 12 小時的窗口中不會從後端多次獲取配置,無論實際進行了多少次獲取調用。具體來說,最小抓取間隔按以下順序確定:

  1. Settings.minimumFetchIntervalMillis中的參數。
  2. 默認值為 12 小時。

下一步

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: