Firebase is back at Google I/O on May 10! Register now

遠程配置觸發器

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

您可以觸發函數以響應Firebase 遠程配置事件,包括發布新配置版本或回滾到舊版本。本指南描述瞭如何創建一個 Remote Config 後台函數來執行兩個模板版本的差異。

觸發遠程配置功能

要觸發遠程配置功能,請使用firebase-functions/v2/remoteConfig子包。要以本指南中所示的方式響應模板更新,您需要使用onConfigUpdated函數,將其與其他所需組件(例如 Firebase Admin SDK)一起導入:

// The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onConfigUpdated} = require("firebase-functions/v2/remoteConfig");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to obtain access tokens.
const admin = require("firebase-admin");
admin.initializeApp();
const fetch = require("node-fetch");
const jsonDiff = require("json-diff");

onConfigUpdated返回的TemplateVersion對象包含模板更新的關鍵元數據字段,例如版本號和更新時間。您還可以檢索進行更新的用戶的電子郵件,其中包含姓名和圖像(如果可用)。

下面是一個遠程配置函數的示例,它返回每個更新版本及其替換版本的差異。該函數檢查模闆對象的versionNumber字段並檢索當前(新更新的)版本以及較低的版本號:

exports.showconfigdiff = onConfigUpdated((event) => {
  // Obtain the access token from the admin SDK
  return admin.credential.applicationDefault().getAccessToken()
      .then((accessTokenObj) => {
        return accessTokenObj.access_token;
      })
      .then((accessToken) => {
      // Get the version number from the event object
        const currentVersion = event.data.versionNumber;
        const templatePromises = [];
        templatePromises.push(getTemplate(currentVersion, accessToken));
        templatePromises.push(getTemplate(currentVersion - 1, accessToken));
        // Get the templates
        return Promise.all(templatePromises);
      })
      .then((results) => {
        const currentTemplate = results[0];
        const previousTemplate = results[1];
        // Figure out the differences of the templates
        const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
        // Log the difference
        logger.log(diff);

        return null;
      }).catch((error) => {
        logger.error(error);
        return null;
      });
});

此示例使用json-diffrequest-promise模塊來創建差異並構建請求以獲取模闆對象。對於結合了遠程配置客戶端邏輯和 Firebase 雲消息傳遞的第一代功能示例,請參閱實時傳播遠程配置更新

,

您可以觸發函數以響應Firebase 遠程配置事件,包括發布新配置版本或回滾到舊版本。本指南描述瞭如何創建一個 Remote Config 後台函數來執行兩個模板版本的差異。

觸發遠程配置功能

要觸發遠程配置功能,請使用firebase-functions/v2/remoteConfig子包。要以本指南中所示的方式響應模板更新,您需要使用onConfigUpdated函數,將其與其他所需組件(例如 Firebase Admin SDK)一起導入:

// The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onConfigUpdated} = require("firebase-functions/v2/remoteConfig");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to obtain access tokens.
const admin = require("firebase-admin");
admin.initializeApp();
const fetch = require("node-fetch");
const jsonDiff = require("json-diff");

onConfigUpdated返回的TemplateVersion對象包含模板更新的關鍵元數據字段,例如版本號和更新時間。您還可以檢索進行更新的用戶的電子郵件,其中包含姓名和圖像(如果可用)。

下面是一個遠程配置函數的示例,它返回每個更新版本及其替換版本的差異。該函數檢查模闆對象的versionNumber字段並檢索當前(新更新的)版本以及較低的版本號:

exports.showconfigdiff = onConfigUpdated((event) => {
  // Obtain the access token from the admin SDK
  return admin.credential.applicationDefault().getAccessToken()
      .then((accessTokenObj) => {
        return accessTokenObj.access_token;
      })
      .then((accessToken) => {
      // Get the version number from the event object
        const currentVersion = event.data.versionNumber;
        const templatePromises = [];
        templatePromises.push(getTemplate(currentVersion, accessToken));
        templatePromises.push(getTemplate(currentVersion - 1, accessToken));
        // Get the templates
        return Promise.all(templatePromises);
      })
      .then((results) => {
        const currentTemplate = results[0];
        const previousTemplate = results[1];
        // Figure out the differences of the templates
        const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
        // Log the difference
        logger.log(diff);

        return null;
      }).catch((error) => {
        logger.error(error);
        return null;
      });
});

此示例使用json-diffrequest-promise模塊來創建差異並構建請求以獲取模闆對象。對於結合了遠程配置客戶端邏輯和 Firebase 雲消息傳遞的第一代功能示例,請參閱實時傳播遠程配置更新