Điều kiện kích hoạt Cấu hình từ xa (thế hệ thứ nhất)
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể kích hoạt một hàm để phản hồi các sự kiện Firebase Remote Config, bao gồm cả việc xuất bản một phiên bản cấu hình mới hoặc quay lại phiên bản cũ.
Hướng dẫn này mô tả cách tạo một hàm nền Remote Config thực hiện việc so sánh khác biệt giữa hai phiên bản mẫu.
Kích hoạt một hàm Cấu hình từ xa
Để xác định một trình xử lý cho các sự kiện Remote Config, hãy sử dụng hàm onUpdate() của mô-đun functions.remoteConfig.
Đối tượng TemplateVersion do onUpdate trả về chứa các trường siêu dữ liệu chính cho một bản cập nhật mẫu, chẳng hạn như số phiên bản và thời gian cập nhật.
Bạn cũng có thể truy xuất email của người dùng đã thực hiện nội dung cập nhật, cùng với tên và hình ảnh (nếu có).
Dưới đây là ví dụ về hàm Remote Config trả về chênh lệch của từng phiên bản được cập nhật và phiên bản mà phiên bản đó thay thế. Hàm này kiểm tra trường versionNumber của đối tượng mẫu và truy xuất phiên bản hiện tại (mới cập nhật) cùng với phiên bản thấp hơn một số:
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-09 UTC."],[],[],null,["\u003cbr /\u003e\n\nYou can trigger a function in response to\n[Firebase Remote Config](/docs/remote-config) events, including\nthe publication of a new config version or the rollback to an older version.\nThis guide describes how to create a Remote Config background function\nthat performs a diff of two template versions.\n\nTrigger a Remote Config function\n\nTo define a handler for Remote Config events, use the\n[`functions.remoteConfig`](/docs/reference/functions/firebase-functions.remoteconfig)\nmodule's `onUpdate()` function.\nThe `TemplateVersion` object returned by\n`onUpdate` contains the key metadata\nfields for a template update such as the version number and time of the update.\nYou can also retrieve the email for the user who made the update, with name\nand an image if available.\n\nHere's an example of a Remote Config function that\nreturns a diff of each updated version and the version it replaced. The function\nexamines the `versionNumber` field of the template object and retrieves the\ncurrent (newly updated) version together with the version one number lower: \n\n```gdscript\nexports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata =\u003e {\n return admin.credential.applicationDefault().getAccessToken()\n .then(accessTokenObj =\u003e {\n return accessTokenObj.access_token;\n })\n .then(accessToken =\u003e {\n const currentVersion = versionMetadata.versionNumber;\n const templatePromises = [];\n templatePromises.push(getTemplate(currentVersion, accessToken));\n templatePromises.push(getTemplate(currentVersion - 1, accessToken));\n\n return Promise.all(templatePromises);\n })\n .then(results =\u003e {\n const currentTemplate = results[0];\n const previousTemplate = results[1];\n\n const diff = jsonDiff.diffString(previousTemplate, currentTemplate);\n\n functions.logger.log(diff);\n\n return null;\n }).catch(error =\u003e {\n functions.logger.error(error);\n return null;\n });\n});https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/remote-config-diff/functions/index.js#L25-L51\n```\n\nThis sample uses the [`json-diff`](https://www.npmjs.com/package/json-diff) and\n[`request-promise`](https://www.npmjs.com/package/request-promise) modules to\ncreate the diff and build the request to get the template object. For a sample\nthat incorporates Remote Config client logic as well as Firebase Cloud Messaging,\nsee [Propagate Remote Config updates in real time](/docs/remote-config/propagate-updates-realtime)."]]