Firebase Remote Config
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
处理对 Firebase Remote Config 值的更改。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Go
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
PHP
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Ruby
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides code samples for processing changes to Firebase Remote Config values across multiple languages.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples demonstrate how to log essential details about a Remote Config update, including the update type, origin, and version number.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Cloud Run functions requires setting up Application Default Credentials, as detailed in the "Set up authentication for a local development environment" document.\u003c/p\u003e\n"],["\u003cp\u003eThe document also contains a link to other code samples using the "Google Cloud Sample browser".\u003c/p\u003e\n"]]],[],null,["# Firebase remote config\n\nProcess changes to Firebase remote config values.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Firebase Remote Config Triggers](/functions/1stgendocs/calling/firebase-remote-config)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using CloudNative.CloudEvents;\n using Google.Cloud.Functions.Framework;\n using Google.Events.Protobuf.Firebase.RemoteConfig.V1;\n using Microsoft.Extensions.Logging;\n using System.Threading;\n using System.Threading.Tasks;\n\n namespace FirebaseRemoteConfig;\n\n public class Function : ICloudEventFunction\u003cRemoteConfigEventData\u003e\n {\n private readonly ILogger _logger;\n\n public Function(ILogger\u003cFunction\u003e logger) =\u003e\n _logger = logger;\n\n public Task HandleAsync(CloudEvent cloudEvent, RemoteConfigEventData data, CancellationToken cancellationToken)\n {\n _logger.LogInformation(\"Update type: {origin}\", data.UpdateType);\n _logger.LogInformation(\"Update origin: {origin}\", data.UpdateOrigin);\n _logger.LogInformation(\"Version number: {version}\", data.VersionNumber);\n\n // In this example, we don't need to perform any asynchronous operations, so the\n // method doesn't need to be declared async.\n return Task.CompletedTask;\n }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package helloworld provides a set of Cloud Functions samples.\n package helloworld\n\n import (\n \t\"context\"\n \t\"log\"\n )\n\n // A RemoteConfigEvent is an event triggered by Firebase Remote Config.\n type RemoteConfigEvent struct {\n \tUpdateOrigin string `json:\"updateOrigin\"`\n \tUpdateType string `json:\"updateType\"`\n \tUpdateUser struct {\n \t\tEmail string `json:\"email\"`\n \t\tImageURL string `json:\"imageUrl\"`\n \t\tName string `json:\"name\"`\n \t} `json:\"updateUser\"`\n \tVersionNumber string `json:\"versionNumber\"`\n }\n\n // HelloRemoteConfig handles Firebase Remote Config events.\n func HelloRemoteConfig(ctx context.Context, e RemoteConfigEvent) error {\n \tlog.Printf(\"Update type: %v\", e.UpdateType)\n \tlog.Printf(\"Origin: %v\", e.UpdateOrigin)\n \tlog.Printf(\"Version: %v\", e.VersionNumber)\n \treturn nil\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.functions.Context;\n import com.google.cloud.functions.RawBackgroundFunction;\n import com.google.gson.Gson;\n import com.google.gson.JsonObject;\n import java.util.logging.Logger;\n\n public class FirebaseRemoteConfig implements RawBackgroundFunction {\n private static final Logger logger = Logger.getLogger(FirebaseRemoteConfig.class.getName());\n\n // Use GSON (https://github.com/google/gson) to parse JSON content.\n private static final Gson gson = new Gson();\n\n @Override\n public void accept(String json, Context context) {\n JsonObject body = gson.fromJson(json, JsonObject.class);\n\n if (body != null) {\n if (body.has(\"updateType\")) {\n logger.info(\"Update type: \" + body.get(\"updateType\").getAsString());\n }\n if (body.has(\"updateOrigin\")) {\n logger.info(\"Origin: \" + body.get(\"updateOrigin\").getAsString());\n }\n if (body.has(\"versionNumber\")) {\n logger.info(\"Version: \" + body.get(\"versionNumber\").getAsString());\n }\n }\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Background Function triggered by a change to a Firebase Remote Config value.\n *\n * @param {object} event The Cloud Functions event.\n */\n exports.helloRemoteConfig = event =\u003e {\n console.log(`Update type: ${event.updateType}`);\n console.log(`Origin: ${event.updateOrigin}`);\n console.log(`Version: ${event.versionNumber}`);\n };\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Google\\CloudFunctions\\CloudEvent;\n\n function firebaseRemoteConfig(CloudEvent $cloudevent)\n {\n $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');\n\n $data = $cloudevent-\u003egetData();\n\n fwrite($log, 'Update type: ' . $data['updateType'] . PHP_EOL);\n fwrite($log, 'Origin: ' . $data['updateOrigin'] . PHP_EOL);\n fwrite($log, 'Version: ' . $data['versionNumber'] . PHP_EOL);\n }\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n def hello_remote_config(data, context):\n \"\"\"Triggered by a change to a Firebase Remote Config value.\n Args:\n data (dict): The event payload.\n context (google.cloud.functions.Context): Metadata for the event.\n \"\"\"\n print(f'Update type: {data[\"updateType\"]}')\n print(f'Origin: {data[\"updateOrigin\"]}')\n print(f'Version: {data[\"versionNumber\"]}')\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n # Triggered by a change to a Firebase Remote Config value\n FunctionsFramework.cloud_event \"hello_remote_config\" do |event|\n # Event-triggered Ruby functions receive a CloudEvents::Event::V1 object.\n # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html\n # The Firebase event payload can be obtained from the event data.\n payload = event.data\n\n logger.info \"Update type: #{payload['updateType']}\"\n logger.info \"Origin: #{payload['updateOrigin']}\"\n logger.info \"Version: #{payload['versionNumber']}\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]