遠端設定範本和版本管理


遠端設定範本是一組 JSON 格式的 您建立的參數和條件。個人中心 您可以建立用戶端範本,供應用程式從中擷取值,並且 server 範本,可供伺服器用戶端擷取值。

本節會探討用戶端範本。瞭解伺服器專屬 範本,點選 伺服器範本

使用 Firebase 控制台修改和管理範本,畫面會顯示 指定範本中以圖形格式呈現的內容 參數和 「條件」分頁。

您也可以使用 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 控制台執行下列版本管理工作:

您也可以使用範本列出範本、擷取範本及復原範本 Firebase CLI 和遠端設定後端 API

每個範本類型最多可以有 300 個生命週期儲存版本 (300 個用戶端範本和 300 個伺服器範本),其中包含 已刪除範本的版本號碼。超過 300 次 專案生命週期內每個範本類型的範本版本 系統會刪除最早的版本,但最多能保有 300 個版本 就可以選擇那些類型的物件

每次更新參數時,遠端設定都會建立 並儲存先前的範本 以便視需要擷取或復原為版本版本號碼 的遞增順序是從遠端設定儲存的初始值之後遞增。 所有範本皆包含 version 欄位 (如圖所示),其中包含該欄位的相關中繼資料 特定版本的 Pod

如有需要,您可以在 變更記錄 CANNOT TRANSLATE 存取遠端設定控制台

管理遠端設定範本版本

本節說明如何管理遠端設定的版本 範本。

如要進一步瞭解建立方式 透過程式輔助方式修改並儲存範本,請參閱 透過程式修改遠端設定

列出遠端設定範本的所有已儲存版本

您可以使用 所有已儲存的遠端設定範本版本現在說明一下操作方式:

Firebase 控制台

在「Parameters」分頁中 選取「時鐘」右上方的圖示。系統隨即會開啟 變更記錄 頁面右側會列出所有儲存的範本版本。

為每個儲存的版本顯示的詳細資料,包括 透過 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"
  }]
}
```

擷取特定版本的遠端設定範本

您可以擷取 遠端設定範本的版本擷取儲存的範本 版本:

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 的要求 參數就會擷取目前使用中的範本。

將遠端設定範本復原至特定已儲存的版本

您隨時可以復原至任何已儲存的 範本版本如何復原範本:

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

如要復原至已儲存的遠端設定範本,請發出以下狀態的 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) 會變成有效的範本

刪除遠端設定範本

您可以透過 Firebase 控制台刪除遠端設定範本。目的地: 刪除遠端設定範本:

1.使用遠端設定 參數 頁面,按一下 變更記錄
  1. 切換到要刪除的範本,然後按一下 請選取 [更多],然後選取 刪除

  2. 系統提示您確認刪除時,按一下「Delete」(刪除)

下載並發布遠端設定範本

下載並發布遠端設定範本,將範本整合至 原始碼控制及建構系統、自動設定更新,以及保留參數 來同步多個專案的

您可以下載目前有效的遠端設定範本 以程式輔助的方式 之後,您就能更新 匯出 JSON 檔案並發布至相同專案,或發布至新的專案, 現有專案

假設您有多個專案 分別代表了不同的應用程式階段 軟體開發的生命週期,例如開發、測試、測試和正式環境 環境在這個例子中,您可以把經過完整測試的範本 從 測試環境並發布至正式版專案。

您也可以使用這個方法將設定從一項專案遷移至 也可以用來自 專案。

專為以下項目建立的參數和參數值: 匯出的範本中沒有 A/B 測試實驗。

如何匯出及匯入遠端設定範本:

  1. 下載目前的遠端設定設定範本
  2. 驗證遠端設定範本
  3. 發布遠端設定範本

下載目前的遠端設定範本

透過下列指令,下載有效的遠端設定範本: JSON 格式:

Firebase 控制台

  1. 遠端設定參數或條件 開啟 「選單」, 選取「下載目前的設定檔」
  2. 系統顯示提示時,請點選「下載設定檔」,選擇儲存位置 然後按一下「儲存」即可儲存檔案。

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,並已更新 ETag (後置字串為 -0)。如果您的 範本未經驗證,您會在 JSON 回應和您的 headers 檔案將包含非 200 回應 (且沒有 ETag)。

發布遠端設定範本

下載範本後,對 JSON 內容進行必要變更 您就可以將其發布到專案

發布範本後,系統會將整個現有設定範本替換成 更新 檔案,並將範本版本 1 遞增。由於整個 設定替換,如果您從 JSON 檔案刪除參數 該參數就會從伺服器中刪除,並且不再提供 。

發布後,參數和值的異動立即可用 為應用程式和使用者提供服務如有需要,您可以 復原為先前版本

使用下列指令發布範本:

Firebase 控制台

  1. 遠端設定參數或條件 開啟「選單」分頁, 然後選取「透過檔案發布」
  2. 系統提示時,按一下「Browse」(瀏覽),瀏覽至並選取 要發布的遠端設定檔,然後按一下「選取」
  3. 檔案會通過驗證,如果成功,你可以按一下 發布:將設定立即提供給 應用程式和使用者

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 指令,您可以使用「@」來指定內容 字元,後面加上檔案名稱。

遠端設定個人化功能和條件已納入 下載範本,因此請務必留意下列事項 發布至其他專案的限制:

  • 無法將個人化作業從專案匯入專案。

    舉例來說,如果專案啟用了個人化功能, 下載並編輯範本 就能將範本發布在 但無法發布至其他專案 套用範本的個人化設定

  • 條件可從專案匯入至專案,但請注意 特定條件值 (例如應用程式 ID 或目標對象) 應存在於 在發布前就先選取目標專案

    舉例來說,如果您有一個使用條件的遠端設定參數 指定平台值 iOS 時,範本可發布至 其他專案的平台值都一樣。 但是,如果條件需要使用特定應用程式 ID 或使用者, 如果目標對像不存在於目標專案中,驗證就會失敗。

  • 如果您要發布的範本包含 Google Analytics,您必須在目標中啟用 Analytics 專案。

下載遠端設定範本預設值

因為應用程式不一定每次都能連上網際網路 針對所有遠端設定,設定用戶端應用程式預設值 參數。建議您也定期同步處理應用程式用戶端預設值 和遠端設定後端預設參數值 可能會隨時間改變。

如本節結尾各平台的專屬連結所述, 或是在應用程式中手動設定這些預設值 下載「只」包含所有參數鍵/值組合的檔案 和相應的預設值接著 將這個檔案納入專案,並設定應用程式來匯入這些值。

您可以在 Android 應用程式、Android 應用程式 iOS 應用程式的屬性清單 (plist) 格式,以及適用於網頁應用程式的 JSON 格式。

建議您先定期下載遠端設定預設值,再建立新的叢集 版本,藉此確保應用程式和遠端設定後端 同步處理。

如何下載內含範本預設值的檔案:

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'

依據檔案,使用 XMLPLISTJSON 做為 format 值 格式的檔案。

Firebase 控制台

  1. 在「Parameters」分頁中 開啟選單,然後選取 下載預設值
  2. 在系統提示時,按一下與檔案對應的圓形按鈕 格式,然後點選「下載檔案」

若要進一步瞭解如何將遠端設定預設值匯入您的 應用程式,請參閱: