【问题标题】:Listen to changes in Root collection cloud function in firestore监听firestore中Root集合云功能的变化
【发布时间】:2019-04-02 07:43:53
【问题描述】:

我看到的大多数示例都解释了如何使用用户的 uid 收听文档。我正在尝试只收听一般父集合

exports.sendNotifications = functions.firestore.document('Notifications').onCreate(async (snapshot) => {
    // Notification details.
    const payload = {
      notification: {
          title: 'Hello',
          body: 'Hello again',
        click_action: `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`,
      }
    };

    // Get the list of device tokens.
    const allTokens = await admin.firestore().collection('fcmTokens').get();
    const tokens = [];
    allTokens.forEach((tokenDoc) => {
      tokens.push(tokenDoc.id);
    });

    if (tokens.length > 0) {
      // Send notifications to all tokens.
      const response = await admin.messaging().sendToDevice(tokens, payload);

    }
  });

此云功能带来functions: failed to create function sendNotifications HTTP Error: 400, The request has errors。我猜的错误是因为没有正确引用firestore集合。它是根集合。我怎样才能更好地参考它

【问题讨论】:

  • 不清楚你说的是哪个系列。您能否添加数据模型的详细信息,并提供有关何时触发 Cloud Function 的更多信息(在 Notifications 集合中创建文档时?
  • 我需要在添加文档时收听 Notifications 集合,然后向我拥有令牌的用户发送 fcm 通知。
  • 通知(根集合)-> 文档和 fcmTokens(根集合)-> 文档。数据模型就是这么简单

标签: javascript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

您应该在 Cloud Function 中修改以下几点:

1/ 您应该在创建 Document 时触发它,如下所示。见https://firebase.google.com/docs/functions/firestore-events?authuser=0#wildcards-parameters

exports.sendNotifications = functions.firestore.document('Notifications/{notifId}')
    .onCreate(async (snap, context) => {

      const newValue = snap.data();

      // perform desired operations ...
      // You may not need the notifId value but you have to keep the wildcard in the document path
    });

2/ 另外,请注意onCreate() 有一个data 和一个context 参数。有关详细信息,请参阅 https://firebase.google.com/docs/functions/firestore-events?authuser=0#trigger_a_function_when_a_new_document_is_createdhttps://firebase.google.com/docs/functions/beta-v1-diff?authuser=0#cloud-firestore

3/ 最后,您应该返回由admin.messaging() 异步任务返回的promise,并在tokens.length = 0 的情况下返回一个值。这两个操作确保您向平台指示 Cloud Function 的工作已完成。 (我建议您观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频:https://firebase.google.com/docs/functions/video-series/

因此,最后您的代码将如下所示。 (注意我没有测试过,所以我不能100%保证它会解决你的“HTTP Error: 400, The request has errors”问题...)

exports.sendNotifications = functions.firestore.document('Notifications/{notifId}')
.onCreate(async (snap, context) => {
    // Notification details.
    const payload = {
      notification: {
          title: 'Hello',
          body: 'Hello again',
        click_action: `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`,
      }
    };

    // Get the list of device tokens.
    const allTokens = await admin.firestore().collection('fcmTokens').get();
    const tokens = [];
    allTokens.forEach((tokenDoc) => {
      tokens.push(tokenDoc.id);
    });

    if (tokens.length > 0) {
      // Send notifications to all tokens.
      return await admin.messaging().sendToDevice(tokens, payload);
    } else {
      return null;
    }
  });

【讨论】:

  • 谢谢你这个解释很清楚,有很好的参考,特别是对于像我这样刚学习firebase的人
猜你喜欢
  • 2020-10-27
  • 2020-05-18
  • 2021-03-06
  • 2023-04-10
  • 2019-02-17
  • 1970-01-01
  • 2012-03-30
  • 2021-03-10
  • 2020-03-15
相关资源
最近更新 更多