Firebase 遠端設定觸發條件
您可以依據 Firebase 遠端設定的變更,觸發與函式位於相同 Google Cloud 專案中的 Cloud Run 函式。這樣一來,您就能變更應用程式的行為和外觀,而不必發布應用程式更新。
事件類型
Firebase 遠端設定可根據 remoteconfig.update
事件觸發函式。
事件類型 | 觸發條件 |
---|---|
remoteconfig.update |
在更新遠端設定範本時觸發。 |
事件結構
事件資料會以轉換後的 remoteConfig
物件形式提供。
例如:
{ "updateType": "FORCED_UPDATE", "updateOrigin": "CONSOLE", "versionNumber": 1 }
程式碼範例
Node.js
Python
Go
Java
C#
using CloudNative.CloudEvents; using Google.Cloud.Functions.Framework; using Google.Events.Protobuf.Firebase.RemoteConfig.V1; using Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; namespace FirebaseRemoteConfig; public class Function : ICloudEventFunction<RemoteConfigEventData> { private readonly ILogger _logger; public Function(ILogger<Function> logger) => _logger = logger; public Task HandleAsync(CloudEvent cloudEvent, RemoteConfigEventData data, CancellationToken cancellationToken) { _logger.LogInformation("Update type: {origin}", data.UpdateType); _logger.LogInformation("Update origin: {origin}", data.UpdateOrigin); _logger.LogInformation("Version number: {version}", data.VersionNumber); // In this example, we don't need to perform any asynchronous operations, so the // method doesn't need to be declared async. return Task.CompletedTask; } }
Ruby
PHP
use Google\CloudFunctions\CloudEvent; function firebaseRemoteConfig(CloudEvent $cloudevent) { $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb'); $data = $cloudevent->getData(); fwrite($log, 'Update type: ' . $data['updateType'] . PHP_EOL); fwrite($log, 'Origin: ' . $data['updateOrigin'] . PHP_EOL); fwrite($log, 'Version: ' . $data['versionNumber'] . PHP_EOL); }
部署函式
如要部署函式,您必須指定事件類型 google.firebase.remoteconfig.update
。
下列 gcloud
指令會部署由 Firebase 遠端設定事件觸發的函式:
gcloud functions deploy FUNCTION_NAME \ --no-gen2 \ --entry-point ENTRY_POINT \ --trigger-event google.firebase.remoteconfig.update \ --runtime RUNTIME
引數 | 說明 |
---|---|
FUNCTION_NAME |
您要部署的 Cloud Run 函式註冊名稱。這可以是原始碼中的函式名稱,也可以是任意字串。如果 FUNCTION_NAME 是任意字串,則必須加入 --entry-point 旗標。 |
--entry-point ENTRY_POINT |
原始碼中函式或類別的名稱。選用,除非您未使用 FUNCTION_NAME 指定要於部署期間執行的原始碼中的函式。在這種情況下,您必須使用 --entry-point 提供可執行函式的名稱。 |
--trigger-event google.firebase.remoteconfig.update |
在 Firebase 遠端設定更新事件時觸發函式。 |
--runtime RUNTIME |
您使用的執行階段名稱。如需完整清單,請參閱 gcloud 參考資料。 |