Firebase Remote Config 更改触发器

此示例演示了如何在 Firebase Remote Config 值发生更改时触发 Cloud Functions 函数。

代码示例

Go

如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


// Package remote_config provides cloud event sample for firebase remote config updates.
package remoteconfig

import (
	"context"
	"fmt"
	"log"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
	"github.com/cloudevents/sdk-go/v2/event"
	"github.com/googleapis/google-cloudevents-go/firebase/remoteconfigdata"
	"google.golang.org/protobuf/encoding/protojson"
)

func init() {
	functions.CloudEvent("HelloRemoteConfig", HelloRemoteConfig)
}

// HelloRemoteConfig handles Firebase Remote Config events.
func HelloRemoteConfig(ctx context.Context, e event.Event) error {
	unmarshalOptions := protojson.UnmarshalOptions{DiscardUnknown: true}

	var data remoteconfigdata.RemoteConfigEventData
	if err := unmarshalOptions.Unmarshal(e.Data(), &data); err != nil {
		return fmt.Errorf("UnmarshalTo: %w", err)
	}

	log.Printf("Update type: %+v", data.GetUpdateType())
	log.Printf("Origin: %+v", data.GetUpdateOrigin())
	log.Printf("Version: %+v", data.GetVersionNumber())
	return nil
}

Java

如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.firebase.remoteconfig.v1.RemoteConfigEventData;
import com.google.protobuf.util.JsonFormat;
import io.cloudevents.CloudEvent;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

public class FirebaseRemoteConfig implements CloudEventsFunction {
  private static final Logger logger = Logger.getLogger(FirebaseRemoteConfig.class.getName());

  @Override
  public void accept(CloudEvent event) throws Exception {
    if (event.getData() == null) {
      logger.info("No data found in event");
      return;
    }

    RemoteConfigEventData.Builder builder = RemoteConfigEventData.newBuilder();
    JsonFormat.Parser jsonParser = JsonFormat.parser().ignoringUnknownFields();
    jsonParser.merge(new String(event.getData().toBytes(), StandardCharsets.UTF_8), builder);
    RemoteConfigEventData data = builder.build();

    logger.info("Update type: " + data.getUpdateType().name());
    logger.info("Origin: " + data.getUpdateOrigin().name());
    logger.info("Version: " + data.getVersionNumber());
  }
}

Node.js

如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

const functions = require('@google-cloud/functions-framework');

/**
 * Cloud Event Function triggered by a change to a Firebase Remote Config value.
 *
 * @param {object} event The Cloud Functions event.
 */
functions.cloudEvent('helloRemoteConfig', event => {
  console.log(`Update type: ${event.data.updateType}`);
  console.log(`Origin: ${event.data.updateOrigin}`);
  console.log(`Version: ${event.data.versionNumber}`);
});

Python

如需向 Cloud Run functions 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

from cloudevents.http import CloudEvent
import functions_framework


@functions_framework.cloud_event
def hello_remote_config(event: CloudEvent) -> None:
    """Triggered by a change to a Firebase Remote Config value.
    Args:
        event: Cloud Event triggered by change to Firebase Remote Config
    """
    print(f'Update type: {event.data["updateType"]}')
    print(f'Origin: {event.data["updateOrigin"]}')
    print(f'Version: {event.data["versionNumber"]}')

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器