您可以使用 Firebase 遠程配置在應用中定義參數並在雲中更新它們的值,從而允許您修改應用的外觀和行為,而無需分發應用更新。本指南將引導您完成入門步驟並提供一些示例代碼,所有這些都可以從firebase/quickstart-js GitHub 存儲庫克隆或下載。
添加並初始化遠程配置 SDK
如果您還沒有,請安裝 Firebase JS SDK 並初始化 Firebase 。
添加 Remote Config JS SDK 並初始化 Remote Config:
Web version 9
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 version 8
import firebase from "firebase/app"; import "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 firebase.initializeApp(firebaseConfig); // Initialize Remote Config and get a reference to the service const remoteConfig = firebase.remoteConfig();
此對像用於存儲應用內默認參數值、從遠程配置後端獲取更新的參數值以及控制獲取的值何時可用於您的應用。
設置最小獲取間隔
在開發過程中,建議設置一個相對較低的最小獲取間隔。有關詳細信息,請參閱節流。
Web version 9
remoteConfig.settings.minimumFetchIntervalMillis = 3600000;
Web version 8
remoteConfig.settings.minimumFetchIntervalMillis = 3600000;
設置應用內默認參數值
您可以在遠程配置對像中設置應用內默認參數值,以便您的應用在連接到遠程配置後端之前按預期運行,並且如果後端沒有設置默認值,則可以使用默認值。
Web version 9
remoteConfig.defaultConfig = { "welcome_message": "Welcome" };
Web version 8
remoteConfig.defaultConfig = { "welcome_message": "Welcome" };
如果您已經配置了 Remote Config 後端參數值,您可以下載包含所有默認值的生成 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 控制台
- 在參數選項卡中,打開 菜單,然後選擇下載默認值。
- 出現提示時,啟用.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;
獲取要在您的應用中使用的參數值
現在您可以從遠程配置對像中獲取參數值。如果您稍後在後端設置值,獲取它們,然後激活它們,這些值可用於您的應用程序。要獲取這些值,請調用getValue()
方法,提供參數鍵作為參數。
Web version 9
import { getValue } from "firebase/remote-config"; const val = getValue(remoteConfig, "welcome_messsage");
Web version 8
const val = remoteConfig.getValue("welcome_messsage");
設置參數值
使用 Firebase 控制台或Remote Config 後端 API ,您可以創建新的服務器端默認值,根據您所需的條件邏輯或用戶定位覆蓋應用內值。本部分將引導您完成創建這些值的 Firebase 控制台步驟。
- 在Firebase 控制台中,打開您的項目。
- 從菜單中選擇遠程配置以查看遠程配置儀表板。
- 定義與您在應用程序中定義的參數同名的參數。對於每個參數,您可以設置一個默認值(最終將覆蓋應用內默認值),您還可以設置條件值。要了解更多信息,請參閱遠程配置參數和條件。
獲取和激活值
- 要從遠程配置後端獲取參數值,請調用
fetchConfig()
方法。您在後端設置的任何值都會被獲取並緩存在遠程配置對像中。 - 要使獲取的參數值可用於您的應用程序,請調用
activate()
方法。
對於要在一次調用中獲取和激活值的情況,請使用fetchAndActivate()
,如下例所示:
Web version 9
import { fetchAndActivate } from "firebase/remote-config"; fetchAndActivate(remoteConfig) .then(() => { // ... }) .catch((err) => { // ... });
Web version 8
remoteConfig.fetchAndActivate() .then(() => { // ... }) .catch((err) => { // ... });
由於這些更新的參數值會影響應用程序的行為和外觀,因此您應該在確保用戶獲得流暢體驗的時間(例如用戶下次打開您的應用程序時)激活獲取的值。有關更多信息和示例,請參閱遠程配置加載策略。
節流
如果應用在短時間內獲取太多次,獲取調用可能會受到限制。在這種情況下,SDK 會引發FETCH_THROTTLE
錯誤。建議您捕獲此錯誤並在指數退避模式下重試,在後續獲取請求之間等待更長的時間間隔。
在應用程序開發期間,您可能希望非常頻繁地刷新緩存(每小時多次),以便在開發和測試應用程序時快速迭代。為了適應具有眾多開發人員的項目的快速迭代,您可以在您的應用程序中臨時添加一個具有較低最小獲取間隔 ( Settings.minimumFetchIntervalMillis
) 的屬性。
遠程配置的默認和推薦生產獲取間隔為 12 小時,這意味著在 12 小時窗口內不會多次從後端獲取配置,無論實際進行了多少次獲取調用。具體來說,最小獲取間隔按以下順序確定:
-
Settings.minimumFetchIntervalMillis
中的參數。 - 默認值為 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: