| 選取範本類型: | 用戶端範本 伺服器範本 |
Remote Config 範本是為 Firebase 專案建立的一組 JSON 格式參數和條件。用戶端範本會將設定提供給實作 Remote Config 專用 Firebase 用戶端 SDK 的應用程式執行個體,包括 Apple、Android、網頁、Flutter、Unity 和 C++ 應用程式。
您可以使用 Firebase 控制台修改及管理用戶端範本。依序前往「DevOps & Engagement」 >「遠端設定」,即可在「參數」頁面和「條件」頁面,以圖形格式查看範本內容。
您也可以使用 Remote Config REST API 和 Admin SDK 或 Firebase CLI 修改及管理用戶端範本。
以下是客戶範本檔案範例:
{
"conditions": [
{
"name": "ios",
"expression": "device.os == 'ios'"
}
],
"parameters": {
"welcome_message": {
"defaultValue": {
"value": "Welcome to this sample app"
},
"conditionalValues": {
"ios": {
"value": "Welcome to this sample iOS app"
}
}
},
"welcome_message_caps": {
"defaultValue": {
"value": "false"
}
},
"header_text": {
"defaultValue": {
"useInAppDefault": true
}
}
},
"version": {
"versionNumber": "28",
"updateTime": "2020-05-14T18:39:38.994Z",
"updateUser": {
"email": "user@google.com"
},
"updateOrigin": "CONSOLE",
"updateType": "INCREMENTAL_UPDATE"
}
}
您可以在 Firebase 控制台中執行這些版本管理工作 (依序前往「DevOps & Engagement」> 「Remote Config」):
- 列出所有儲存的範本版本
- 擷取特定版本
- 復原至特定用戶端版本
- 從「Change history」(變更記錄) 頁面刪除 Remote Config 個範本。
您也可以使用 Firebase CLI 和 Remote Config 後端 API 列出、擷取及復原範本。
每種範本類型 (用戶端範本和伺服器範本各 300 個) 的永久儲存版本總數上限為 300 個,包括已刪除範本的儲存版本數量。如果在專案生命週期內,每種範本類型發布超過 300 個範本版本,系統會刪除最早的版本,最多保留 300 個該類型的版本。
每次更新參數時,Remote Config 都會建立新的 Remote Config 範本版本,並將先前的範本儲存為版本,以便您視需要擷取或復原。版本編號會從 Remote Config 儲存的初始值開始依序遞增。所有範本都包含 version 欄位 (如圖所示),其中包含該特定版本的中繼資料。
您可以視需要刪除 Remote Config 範本,方法是前往 Firebase 控制台的 「DevOps & Engagement」>「遠端設定」> 「變更記錄頁面」。
管理 Remote Config 個範本版本
本節說明如何管理 Remote Config 範本的版本。
如要進一步瞭解如何透過程式輔助方式建立、修改及儲存範本,請參閱「透過程式輔助方式修改 Remote Config」。
列出所有儲存的 Remote Config 範本版本
您可以擷取 Remote Config 範本所有儲存版本的清單。步驟如下:
Firebase 控制台
在 Firebase 控制台中,依序前往「DevOps & Engagement」 >「遠端設定」 >「Parameters」(參數) 頁面。
選取右上方的「時鐘」圖示。
「Change history」(變更記錄) 頁面隨即開啟,右側的清單選單會列出所有已儲存的範本版本。
每個儲存版本顯示的詳細資料,包括變更是否源自 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 token" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:listVersions
範本清單包含所有儲存版本的中繼資料,包括更新時間、更新者和更新方式。以下是版本元素的範例:
{
"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 控制台中,依序前往 「DevOps & Engagement」(開發運作與參與度) >「Remote Config」(遠端設定) > 「Change history」(變更記錄) 頁面。
根據預設,這個頁面會顯示目前有效的範本。如要查看其他版本,請從右側的詳細資料窗格中選取。
將滑鼠游標懸停在任何未選取版本的內容選單上,然後選取「Compare with selected version」(與所選版本比較),即可查看有效範本與任何其他儲存版本的詳細差異。
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 token" -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig?version_number=6
網址參數 ?version_number 僅適用於 GET 作業,無法用於指定更新的版本號碼。如果 get 要求不含 ?version_number 參數,則會擷取目前有效的範本。
復原至 Remote Config 範本的特定儲存版本
您可以復原為任何已儲存的範本版本。復原範本的方法如下:
Firebase 控制台
在 Firebase 控制台中,依序前往 「DevOps & Engagement」(開發運作與參與度) >「Remote Config」(遠端設定) > 「Change history」(變更記錄) 頁面。
在頁面右上方,找出可復原的舊版範本選項按鈕。
只有在確定要復原至該版本,並立即為所有應用程式和使用者套用這些值時,才點選並確認這項操作。
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 範本,請使用自訂方法 :rollback 發出 HTTP POST,並在要求主體中指定要套用的版本。例如:
curl --compressed -D headers -H "Authorization: Bearer token" -H "Content-Type: application/json" -X POST https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:rollback -d '{"version_number": 6}'回應包含目前有效的已儲存範本內容,以及新版本中繼資料。
請注意,這項復原作業實際上會建立新的編號版本。舉例來說,從第 10 版復原至第 6 版,實際上會建立第 6 版的新副本,與原始版本的唯一差異在於版本編號為 11。假設原始版本 6 尚未到期,則仍會儲存,而版本 11 則會成為使用中的範本。
刪除Remote Config範本
您可以使用 Firebase 控制台刪除 Remote Config 範本。刪除範本的方法如下:
在 Firebase 控制台中,依序前往「DevOps & Engagement」 >「遠端設定」 >「Parameters」(參數) 頁面。
按一下
「Change history」(變更記錄) 。切換至要刪除的範本,依序按一下「
更多」 和「刪除」。系統提示確認刪除時,請按一下「刪除」。
下載並發布 Remote Config 範本
下載並發布 Remote Config 範本,將範本整合至原始碼控制和建構系統、自動更新設定,以及在多項專案中同步參數和值。
您可以透過程式輔助方式或 Firebase 控制台,下載目前有效的 Remote Config 範本。接著,您可以更新匯出的 JSON 檔案,並發布至同一個專案,或發布至新的或現有的專案。
舉例來說,假設您有多個專案代表軟體開發生命週期的不同階段,例如開發、測試、預備和正式環境。在這種情況下,您可以從測試專案下載經過完整測試的範本,並發布至正式環境專案,將範本從測試環境升級至正式環境。
您也可以使用這個方法,將設定從一個專案遷移到另一個專案,或使用既有專案的參數和值填入新專案。
在A/B Testing實驗中,專門建立為變化版本的參數和參數值不會包含在匯出的範本中。
如要匯出及匯入Remote Config範本,請按照下列步驟操作:
下載目前的 Remote Config 範本
請使用下列指令,下載 JSON 格式的使用中 Remote Config 範本:
Firebase 控制台
在 Firebase 控制台中,依序前往「DevOps & Engagement」 >「遠端設定」 >「參數或條件」頁面。
開啟
「 選單」 ,然後選取「下載目前的設定檔」。系統提示時,按一下「Download config file」(下載設定檔),選擇要儲存檔案的位置,然後按一下「Save」(儲存)。
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 檔案。
驗證 Remote Config 範本
您可以使用 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 和更新後的 ETag (後置字串為 -0)。如果範本未通過驗證,您會在 JSON 回應中收到驗證錯誤,且 headers 檔案會包含非 200 回應 (且沒有 ETag)。
發布 Remote Config 範本
下載範本、對 JSON 內容進行必要變更並驗證後,即可將範本發布至專案。
發布範本時,系統會以更新後的檔案取代整個現有設定範本,並將範本版本號碼加一。由於整個設定都會被取代,因此如果您從 JSON 檔案中刪除參數並發布,該參數就會從伺服器中刪除,用戶端也無法再使用。
發布後,參數和值的變更會立即套用至應用程式和使用者。如有需要,可以復原至先前版本。
使用下列指令發布範本:
Firebase 控制台
在 Firebase 控制台中,依序前往「DevOps & Engagement」 >「遠端設定」 >「參數或條件」頁面。
開啟
「」選單 ,然後選取「從檔案發布」。出現提示時,按一下「Browse」(瀏覽),前往並選取要發布的 Remote Config 檔案,然後按一下「Select」(選取)。
Firebase 會驗證檔案。如果驗證成功,請按一下「Publish」(發布),立即將設定套用至應用程式和使用者。
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 控制台
在 Firebase 控制台中,依序前往「DevOps & Engagement」 >「遠端設定」 >「Parameters」(參數) 頁面。
開啟
「 選單」 ,然後選取「下載預設值」。系統提示時,請按一下與要下載的檔案格式對應的圓形按鈕,然後按一下「Download file」(下載檔案)。
如要進一步瞭解如何將 Remote Config 預設值匯入應用程式,請參閱: