Google Cloud功能发送重复通知

发布于 2025-02-11 21:22:58 字数 2308 浏览 0 评论 0原文

我有一个发送主题通知的GCF,我会从管理Android应用程序触发该功能。 一切都按预期工作,但是突然间,功能发送了两次通知,有时三遍。 Google Cloud上的函数日志显示该函数已发送一个通知 它仅打印一行:

成功发送了自定义

,但是Android应用程序会收到多个通知,因为onMessageCeived被多次触发。 ,执行时间始终超过15秒

const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
const {getMessaging} = require("firebase-admin/messaging");
const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://mylinktodatabase.firebaseio.com",
});

exports.callNotification = functions.https.onCall( (data) => {
  // Grab the text parameter.
  const indicator = data.indicator;
  const mTitle = data.title;
  const mBody = data.body;
  // topic to send to
  const topic = "mytopic";
  const options = {
    "priority": "high",
    "timeToLive": 3600,
  };
  let message;
  if (indicator != null ) {
    message = {
      data: {
        ind: indicator,
      },
    };
  } else {
    message = {
      data: {
        title: mTitle,
        body: mBody,
      },
    };
  }

  // Send a message to devices subscribed to the provided topic.
  return getMessaging().sendToTopic(topic, message, options)
      .then(() => {
        if (indicator != null ) {
          console.log("Successfully sent message");
          return {
            result: "Successfully sent message", status: 200};
        } else {
          console.log("Successfully sent custom");
          return {
            result: "Successfully sent custom", status: 200};
        }
      })
      .catch((error) => {
        if (indicator != null ) {
          console.log("Error sending message:", error);
          return {result: `Error sending message: ${error}`, status: 500};
        } else {
          console.log("Error sending custom:", error);
          return {result: `Error sending custom: ${error}`, status: 500};
        }
      });
});

另外,我注意到函数发送多个通知时 执行时间,通知何时超过3秒钟,我确认它会发送多个通知。

函数gen为1。 很抱歉在这里提出这个问题,但是Google的支持需要钱来回答哪些令人发指。

I have a gcf that sends a notification for a topic, I trigger the function from an admin android app.
Everything was working as expected, but suddenly the function send the notification two times, sometimes three times.
the function log on google cloud shows the function is sent one notification
it prints only one line of :

Successfully sent custom

But the Android app receives more than one notification because the onMessageReceived is triggered more than once. also, I noticed when the function sent more than one notification the execution time was always more than 15 seconds, and when it sends one notification the execution time was no more than 3 seconds

this is the function's code:

const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
const {getMessaging} = require("firebase-admin/messaging");
const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://mylinktodatabase.firebaseio.com",
});

exports.callNotification = functions.https.onCall( (data) => {
  // Grab the text parameter.
  const indicator = data.indicator;
  const mTitle = data.title;
  const mBody = data.body;
  // topic to send to
  const topic = "mytopic";
  const options = {
    "priority": "high",
    "timeToLive": 3600,
  };
  let message;
  if (indicator != null ) {
    message = {
      data: {
        ind: indicator,
      },
    };
  } else {
    message = {
      data: {
        title: mTitle,
        body: mBody,
      },
    };
  }

  // Send a message to devices subscribed to the provided topic.
  return getMessaging().sendToTopic(topic, message, options)
      .then(() => {
        if (indicator != null ) {
          console.log("Successfully sent message");
          return {
            result: "Successfully sent message", status: 200};
        } else {
          console.log("Successfully sent custom");
          return {
            result: "Successfully sent custom", status: 200};
        }
      })
      .catch((error) => {
        if (indicator != null ) {
          console.log("Error sending message:", error);
          return {result: `Error sending message: ${error}`, status: 500};
        } else {
          console.log("Error sending custom:", error);
          return {result: `Error sending custom: ${error}`, status: 500};
        }
      });
});

this is the image that shows the execution time, notice when it is more than 3 seconds I confirm it sends more than one notification.
enter image description here

Function gen is 1.
sorry to ask this question here, but google support needs money to answer which is outrageous.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2025-02-18 21:22:58

感谢Firebase支持团队。

函数的目的是“至少一次”,单个事件的多次执行是预期的行为,而不是错误,在这种情况下,您可以尝试应用以下建议:

  1. 写下didempotent函数(这是关键点),iDempotent功能是它的位置
    如果执行一次函数,则结果是相同的
    一次。
  2. 使用“ context.eventid”,因为多个执行将具有相同的
    EventID如果是由同一事件产生的,
  3. 则可以检查Firebase中特定事件ID的日志
    日志查看器或Google Cloud Log,以确保它是同一事件。

Thanks to Firebase support team.

Functions aim to be “at least once”, multiple executions for a single event is expected behavior and not a bug, in this case, you may try to apply the following recommendations:

  1. Write Idempotent Functions (this is the key point) , an idempotent function is it where its
    result is the same if the function is executed once, or more than
    once.
  2. Use “context.eventId”, since multiple executions will have the same
    eventId if it resulted from the same event
  3. You can check for the logs of a specific event id in the Firebase
    Logs Viewer or Google cloud log to make sure it was the same event.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文