Remote Config 範本是您為 Firebase 專案建立的 JSON 格式參數和條件集。您可以建立用戶端範本 (應用程式會從中擷取值),以及伺服器範本 (伺服器用戶端會從中擷取值)。
本節將說明用戶端範本。如要瞭解伺服器專屬範本,請按一下「伺服器範本」。您可以使用 Firebase 控制台修改及管理範本,控制台會在「參數」 「參數」和「條件」分頁標籤。
您也可以使用 Remote Config REST API 和 Admin SDK 或 Firebase CLI 修改及管理用戶端範本。
以下是伺服器範本檔案的範例:
{
"parameters": {
"preamble_prompt": {
"defaultValue": {
"value": "You are a helpful assistant who knows everything there is to know about Firebase! "
},
"description": "Add this prompt to the user's prompt",
"valueType": "STRING"
},
"model_name": {
"defaultValue": {
"value": "gemini-pro-test"
},
"valueType": "STRING"
},
"generation_config": {
"defaultValue": {
"value": "{\"temperature\": 0.9, \"maxOutputTokens\": 2048, \"topP\": 0.9, \"topK\": 20}"
},
"valueType": "JSON"
},
},
"version": {
"versionNumber": "19",
"isLegacy": true
}
}
您可以使用 Firebase 控制台執行下列版本管理工作:
每個範本類型最多可儲存 300 個版本 (300 個用戶端範本和 300 個伺服器範本),包括已刪除範本的儲存版本號碼。如果專案生命週期內,每個範本類型發布超過 300 個範本版本,系統會刪除最早的版本,將該類型的版本數量維持在最多 300 個。
每次更新參數時,Remote Config都會建立新的Remote Config範本版本,並將先前的範本儲存為版本,方便您視需要擷取或還原。版本號碼會從 Remote Config 儲存的初始值開始,依序遞增。如圖所示,所有範本都包含 version
欄位,其中含有特定版本的中繼資料。
您可以視需要從 Remote Config 控制台的「變更記錄」頁面刪除 Remote Config 範本。
管理 Remote Config 範本版本
本節說明如何管理Remote Config範本版本。
列出 Remote Config 範本的所有儲存版本
您可以擷取 Remote Config 範本的所有儲存版本清單。現在說明一下操作方式:
Firebase 控制台
在「參數」分頁中,選取右上角顯示的「時鐘」圖示。這會開啟「變更記錄」頁面,右側的清單選單會列出所有儲存的範本版本。
系統會顯示每個儲存版本的詳細資料,包括變更是否源自於主控台、REST API、回溯,或是範本強制儲存的增量變更。
Firebase CLI
firebase remoteconfig:versions:list
使用 --limit
選項限制傳回的版本數量。
如要擷取所有版本,請傳遞「0」。
Node.js
function listAllVersions() {
admin.remoteConfig().listVersions()
.then((listVersionsResult) => {
console.log("Successfully fetched the list of versions");
listVersionsResult.versions.forEach((version) => {
console.log('version', JSON.stringify(version));
});
})
.catch((error) => {
console.log(error);
});
}
Java
ListVersionsPage page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get(); while (page != null) { for (Version version : page.getValues()) { System.out.println("Version: " + version.getVersionNumber()); } page = page.getNextPage(); } // Iterate through all versions. This will still retrieve versions in batches. page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get(); for (Version version : page.iterateAll()) { System.out.println("Version: " + version.getVersionNumber()); }
REST
curl --compressed -D headers -H "Authorization: Bearer <var>token</var>" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/<var>my-project-id</var>/remoteConfig:listVersions
範本清單包含所有儲存版本的相關中繼資料,包括更新時間、更新者和更新方式。版本元素範例如下:
```json
{
"versions": [{
"version_number": "6",
"update_time": "2022-05-12T02:38:54Z",
"update_user": {
"name": "Jane Smith",
"email": "jane@developer.org",
"imageUrl": "https://lh3.googleusercontent.com/a-/..."
},
"description": "One small change on the console",
"origin": "CONSOLE",
"update_type": "INCREMENTAL_UPDATE"
}]
}
```
擷取特定版本的 Remote Config 範本
您可以擷取 Remote Config 範本的任何特定儲存版本。如要擷取儲存的範本版本:
Firebase 控制台
根據預設,「變更記錄」分頁中的詳細資料窗格會顯示目前有效的範本。如要查看清單中其他版本的詳細資料,請從右側選單中選取。
如要查看目前所選版本與任何其他儲存版本的詳細差異,請將滑鼠游標懸停在任何未選取版本的內容選單上,然後選取「與所選版本比較」。
Firebase CLI
firebase remoteconfig:get -v VERSION_NUMBER
您也可以使用 -o, FILENAME
將輸出內容寫入指定檔案。
Node.js
傳遞 getTemplate()
時不帶任何引數,即可擷取最新版本的範本;如要擷取特定版本,請使用 getTemplateAtVersion()
。
// Get template version: 6
admin.remoteConfig().getTemplateAtVersion('6')
.then((template) => {
console.log("Successfully fetched the template with ETag: " + template.etag);
})
.catch((error) => {
console.log(error);
});
Java
Template template = FirebaseRemoteConfig.getInstance().getTemplateAtVersionAsync(versionNumber).get(); // See the ETag of the fetched template. System.out.println("Successfully fetched the template with ETag: " + template.getETag());
REST
curl --compressed -D headers -H "Authorization: Bearer <var>token</var>" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/<var>my-project-id</var>/remoteConfig?version_number=6
網址參數 ?version_number
僅適用於 GET
作業,無法用於指定更新的版本號碼。如果沒有 ?version_number
參數,類似的 get 要求會擷取目前有效的範本。
還原至 Remote Config 範本的特定儲存版本
您可以還原範本的任何儲存版本。如要復原範本,請按照下列步驟操作:
Firebase 控制台
如果先前的範本版本符合回溯條件,系統會在「變更記錄」頁面右上角顯示回溯至該版本的選項按鈕。只有在確定要還原至該版本,並立即將這些值套用至所有應用程式和使用者時,才點選並確認。
Firebase CLI
firebase remoteconfig:rollback -v VERSION_NUMBER
Node.js
// Roll back to template version: 6
admin.remoteConfig().rollback('6')
.then((template) => {
console.log("Successfully rolled back to template version 6.");
console.log("New ETag: " + template.etag);
})
.catch((error) => {
console.log('Error trying to rollback:', e);
})
Java
try { Template template = FirebaseRemoteConfig.getInstance().rollbackAsync(versionNumber).get(); System.out.println("Successfully rolled back to template version: " + versionNumber); System.out.println("New ETag: " + template.getETag()); } catch (ExecutionException e) { if (e.getCause() instanceof FirebaseRemoteConfigException) { FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause(); System.out.println("Error trying to rollback template."); System.out.println(rcError.getMessage()); } }
REST
如要還原為儲存的 Remote Config 範本,請發出 HTTP POST,並使用自訂方法 :rollback
,以及在要求主體中指定要套用的版本。例如:
curl --compressed -D headers -H "Authorization: Bearer <var>token</var>" -H "Content-Type: application/json" -X POST https://firebaseremoteconfig.googleapis.com/v1/projects/<var>my-project-id</var>/remoteConfig:rollback -d '{"version_number": 6}'
回應會包含目前啟用的已儲存範本內容,以及新版中繼資料。
請注意,這項回溯作業實際上會建立新的編號版本。舉例來說,從版本 10 復原至版本 6,實際上會建立版本 6 的新副本,與原始版本唯一的不同之處在於版本號碼為 11。假設原始版本 6 尚未過期,系統仍會儲存該版本,而版本 11 則會成為有效範本。
刪除 Remote Config 範本
您可以從 Firebase 控制台刪除 Remote Config 範本。如要刪除 Remote Config 範本,請按照下列步驟操作:
1. 在「參數」Remote Config 頁面中,按一下 「變更記錄」。切換至要刪除的範本,按一下「更多」圖示
,然後選取「刪除」。系統提示確認刪除時,請按一下「刪除」。
下載及發布 Remote Config 範本
下載並發布Remote Config範本,將其整合至來源控制和建構系統、自動更新設定,並確保多個專案的參數和值保持同步。
您可以透過程式輔助或從 Firebase 控制台下載目前有效的 Remote Config 範本。 接著,您可以更新匯出的 JSON 檔案,並發布至相同專案,或發布至新專案或現有專案。
假設您有多個專案,分別代表軟體開發生命週期的不同階段,例如開發、測試、試行和實際工作環境。在這種情況下,您可以從暫存專案下載經過完整測試的範本,然後發布至實際工作專案,將範本從暫存環境升級至實際工作環境。
您也可以使用這個方法,將設定從一個專案遷移至另一個專案,或使用現有專案的參數和值,填入新專案。
在 A/B Testing 實驗中專門建立為變體的參數和參數值,不會匯出至範本。
如要匯出及匯入 Remote Config 範本,請按照下列步驟操作:
下載目前的遠端設定範本
使用下列項目下載 JSON 格式的有效 Remote Config 範本:
Firebase 控制台
- 在「Parameters」或「Conditions」Remote Config分頁中,開啟「Menu」 ,然後選取「Download current config file」。
- 系統提示時,請按一下「下載設定檔」,選擇要儲存檔案的位置,然後按一下「儲存」。
Firebase CLI
firebase remoteconfig:get -o filename
Node.js
function getTemplate() { var config = admin.remoteConfig(); config.getTemplate() .then(function (template) { console.log('ETag from server: ' + template.etag); var templateStr = JSON.stringify(template); fs.writeFileSync('config.json', templateStr); }) .catch(function (err) { console.error('Unable to get template'); console.error(err); }); }
Java
Template template = FirebaseRemoteConfig.getInstance().getTemplateAsync().get(); // See the ETag of the fetched template. System.out.println("ETag from server: " + template.getETag());
REST
curl --compressed -D headers -H "Authorization: Bearer token" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig -o filename
這個指令會將 JSON 酬載輸出至一個檔案,並將標頭 (包括 ETag) 輸出至另一個 headers
檔案。
驗證遠端設定範本
您可以使用 Firebase Admin SDK 或 REST API,在發布範本更新前進行驗證。嘗試從 Firebase CLI 或 Firebase 控制台發布範本時,系統也會驗證範本。範本驗證程序會檢查錯誤,例如參數和條件的重複鍵、無效的條件名稱或不存在的條件,或是格式錯誤的 ETag。舉例來說,如果要求包含超過允許的鍵數 (2000 個),系統會傳回 Param count too
large
錯誤訊息。
Node.js
function validateTemplate(template) { admin.remoteConfig().validateTemplate(template) .then(function (validatedTemplate) { // The template is valid and safe to use. console.log('Template was valid and safe to use'); }) .catch(function (err) { console.error('Template is invalid and cannot be published'); console.error(err); }); }
Java
try { Template validatedTemplate = FirebaseRemoteConfig.getInstance() .validateTemplateAsync(template).get(); System.out.println("Template was valid and safe to use"); } catch (ExecutionException e) { if (e.getCause() instanceof FirebaseRemoteConfigException) { FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause(); System.out.println("Template is invalid and cannot be published"); System.out.println(rcError.getMessage()); } }
REST
在發布要求中附加網址參數 ?validate_only=true
,驗證範本更新:
curl --compressed -H "Content-Type: application/json; UTF8" -H "If-Match: last-returned-etag" -H "Authorization: Bearer token" -X PUT https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig?validate_only=true -d @filename
如果範本驗證成功,curl 指令會傳回您提交的 JSON 範本,而儲存的 headers
檔案會包含 HTTP/2 狀態 200,以及帶有 -0
後置字元的更新版 ETag。如果範本未通過驗證,您會在 JSON 回應中收到驗證錯誤,且 headers
檔案會包含非 200 的回應 (以及沒有 ETag)。
發布 Remote Config 範本
下載範本、對 JSON 內容進行必要變更並驗證後,即可發布至專案。
發布範本時,系統會以更新後的檔案取代整個現有設定範本,並將範本版本遞增 1。由於系統會取代整個設定,因此如果您從 JSON 檔案刪除參數並發布,該參數就會從伺服器刪除,用戶端也無法再使用。
發布後,參數和值的變更會立即套用至應用程式和使用者。如有必要,可以復原至先前版本。
請使用下列指令發布範本:
Firebase 控制台
- 在「參數」或「條件」Remote Config分頁中,開啟「選單」 ,然後選取「從檔案發布」。
- 出現提示時,按一下「瀏覽」,然後前往並選取要發布的 Remote Config 檔案,再按一下「選取」。
- 系統會驗證檔案,如果驗證成功,您可以按一下「發布」,讓應用程式和使用者立即使用設定。
Node.js
function publishTemplate() { var config = admin.remoteConfig(); var template = config.createTemplateFromJSON( fs.readFileSync('config.json', 'UTF8')); config.publishTemplate(template) .then(function (updatedTemplate) { console.log('Template has been published'); console.log('ETag from server: ' + updatedTemplate.etag); }) .catch(function (err) { console.error('Unable to publish template.'); console.error(err); }); }
Java
try { Template publishedTemplate = FirebaseRemoteConfig.getInstance() .publishTemplateAsync(template).get(); System.out.println("Template has been published"); // See the ETag of the published template. System.out.println("ETag from server: " + publishedTemplate.getETag()); } catch (ExecutionException e) { if (e.getCause() instanceof FirebaseRemoteConfigException) { FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause(); System.out.println("Unable to publish template."); System.out.println(rcError.getMessage()); } }
REST
curl --compressed -H "Content-Type: application/json; UTF8" -H "If-Match: last-returned-etag" -H "Authorization: Bearer token" -X PUT https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig -d @filename
對於這個 curl
指令,你可以使用「@」字元指定內容,後面加上檔案名稱。
Remote Config下載的範本會包含個人化設定和條件,因此嘗試發布至其他專案時,請務必留意下列限制:
個人化設定無法從一個專案匯入另一個專案。
舉例來說,如果您在專案中啟用個人化功能,並下載及編輯範本,可以將範本發布至同一個專案,但除非刪除範本中的個人化設定,否則無法發布至其他專案。
您可以將條件從一個專案匯入另一個專案,但請注意,發布前,目標專案中必須存在任何特定條件值 (例如應用程式 ID 或目標對象)。
舉例來說,如果您有使用條件的 Remote Config 參數,且該條件指定平台值為
iOS
,則範本可以發布至其他專案,因為任何專案的平台值都相同。不過,如果條件依據的特定應用程式 ID 或使用者目標對象不存在於目標專案中,驗證就會失敗。如果您打算發布的範本包含依賴 Google Analytics 的條件,則目標專案必須啟用 Analytics。
下載Remote Config範本預設值
由於應用程式不一定會連上網際網路,您應為所有 Remote Config 參數設定用戶端應用程式預設值。您也應定期同步處理應用程式用戶端預設值和 Remote Config 後端預設參數值,因為這些值可能會隨時間變更。
如本節結尾的平台專屬連結所述,您可以在應用程式中手動設定這些預設值,也可以下載檔案,其中只包含所有參數的鍵/值組合,以及有效 Remote Config 範本中的預設值,藉此簡化這個程序。然後將這個檔案納入專案,並設定應用程式匯入這些值。
您可以下載這些檔案,Android 應用程式的格式為 XML、iOS 應用程式為屬性清單 (plist),網路應用程式則為 JSON。
建議您在發布任何新版應用程式前,先定期下載 Remote Config 預設值,確保應用程式和 Remote Config 後端保持同步。
如要下載含有範本預設值的檔案,請按照下列步驟操作:
REST
curl --compressed -D headers -H "Authorization: Bearer token -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:downloadDefaults?format=file_format'
視要下載的檔案格式而定,請使用 XML
、PLIST
或 JSON
做為 format
值。
Firebase 控制台
如要進一步瞭解如何將 Remote Config 預設值匯入應用程式,請參閱: