无法从HTTPS功能触发Firebase发布/订阅功能

时间:2019-09-11 17:23:28

标签: firebase google-cloud-functions google-cloud-pubsub

我创建了两个Firebase函数-一个是HTTPS,其中我将消息发布到某个主题,而pub / sub函数是我在其中响应发布到该主题的消息。

testPubSub.ts

import { pubsub } from "firebase-functions";
export const testPubSub = pubsub
  .topic("high-scores")
  .onPublish(async (message, context) => {
    console.log("hit test pubsub");
    return null;
  });

testHttps.ts

import { https } from "firebase-functions";
import { messaging } from "firebase-admin";

export const testHookEndpoint = https.onRequest(async (request, response) => {
  const payload = {
    notification: {
      title: "Test title",
      body: "test body"
    }
  };

  const pubsubResponse = await messaging().sendToTopic("high-scores", payload);
  console.log("response from pubsub", pubsubResponse);
  response.send("success");
});

HTTPS函数运行正常(响应200),消息传递在响应中返回消息ID,但是我看不到Firebase控制台中运行的发布/订阅函数。

当我查看GCP控制台时,我看到“高分”已在“发布/订阅”标签中注册为主题,并且我可以通过Google Cloud Scheduler触发项目中的其他发布/订阅功能。 / p>

我不确定要为此缺少什么步骤。

1 个答案:

答案 0 :(得分:3)

messaging().sendToTopic("high-scores", payload)正在使用Firebase Cloud Messaging向订阅了给定主题的移动应用程序发送消息。这与Cloud Pubsub消息传递完全不同。这两种产品实际上没有任何共同点-FCM用于移动应用程序,而pubsub用于服务器。

您需要做的是使用node pubsub SDK将消息发送到您的pubsub主题。

相关问题