【问题标题】:Firebase Cloud Functions for Firestore are not triggeringFirestore 的 Firebase Cloud Functions 未触发
【发布时间】:2018-10-16 18:48:27
【问题描述】:

无法让 Firebase Cloud Functions for Firestore 在我的集合的 onWrite 上触发。尝试为聊天应用设置设备到设备的推送通知。功能已部署并按按需付费计划进行,但不会触发文档更改、更新或“聊天”集合中的创建。 Firebase 云消息传递应该发送推送并写入日志。两者都没有发生。 Push 正在与其他来源合作。

感谢您的帮助,希望设备到设备的推送通知更容易,计划是观看聊天文档并在更新或创建新对话时触发推送通知。接受其他想法。谢谢

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.firestore
  .document('chats/{chatID}')
  .onWrite((data, context) => {
    // Get an object representing the document
    console.log('chat triggered');
    // perform desired operations ...

    // See documentation on defining a message payload.
    var message = {
      notification: {
        title: 'Hello World!',
        body: 'Hello World!'
      },
      topic: context.params.chatID
    };

    // Send a message to devices subscribed to the provided topic.
    return admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
        return true
      })
      .catch((error) => {
        console.log('Error sending message:', error);
      });

  });

更新:我正在使用“firebase-functions”:“^1.0.1”

更新:更新了代码以反映我们当前已部署的内容,但仍然无法正常工作。

【问题讨论】:

  • 您在函数日志中看到错误消息吗?
  • “Can't get the method to trigger a log statement either”究竟是什么意思。在 Firebase 控制台中,在浏览器 (console.firebase.google.com) 中,如果您在垂直菜单中选择“Functions”并选择“LOGS”选项卡,您将看到来自 Functions 的日志。
  • 我不清楚您看到的日志。您是否看到指示 sendNotification Function execution started 的日志?
  • 您如何在chats/{chatID}、代码或 Firebase 控制台上编写 Firestore 数据?如果是代码,请编辑您的帖子以包含它。
  • 我的问题是关于您如何写入 Firestore 数据库,而不是日志。无论您使用哪种方法,您是否看到 Firebase 控制台数据库选项卡中的数据发生变化?

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


【解决方案1】:

对我来说,问题是我正在编写 Firestore 中已经存在的数据。如果您正在写入的数据与那里的数据完全相同,则显然不会触发 onWrite。

【讨论】:

    【解决方案2】:

    您可能会在新库 (v1.0) 中使用旧语法(V1.0 之前)。请参阅迁移指南:https://firebase.google.com/docs/functions/beta-v1-diff 并检查 package.json 文件中的版本。

    此外,请注意云函数必须始终返回一个 Promise(或者如果您不能,至少对于异步函数来说是一个值)。请参阅此文档(和相关视频),其中详细说明了这一点:https://firebase.google.com/docs/functions/terminate-functions

    你应该这样修改你的代码:

    如果您使用的是 Cloud Functions 1.0 或更高版本:

    exports.sendNotification = functions.firestore
        .document('chats/{chatID}')
        .onWrite((change, context) => {
    

    返回:

    exports.sendNotification = functions.firestore
    .document('chats/{chatID}')
    .onWrite((change, context) => {
      // Get an object representing the document
       console.log('chat triggered');
      // perform desired operations ...
    
        // See documentation on defining a message payload.
        var message = {
          notification: {
            title: 'Hello World!',
            body: 'Hello World!'
          },
          topic: context.params.chatID.   //<- If you are using a CF version under v1.0 don't change here
        };
    
        // Send a message to devices subscribed to the provided topic.
        return admin.messaging().send(message).  //<- return the resulting Promise
          .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent message:', response);
            return true;    //<- return a value
          })
          .catch((error) => {
            console.log('Error sending message:', error);
            //return.  <- No need to return here
          });
    
    });
    

    【讨论】:

      【解决方案3】:

      您的firebase-admininitialization syntaxadmin.initializeApp() 表明您使用的是 Cloud Functions SDK 版本 1.0.x。 onWrite() have changed in version 1.0.x 的参数来自早期版本。您还需要return a Promise 进行异步操作admin.messaging.send()。下面列出了三个需要的更正:

      exports.sendNotification = functions.firestore
          .document('chats/{chatID}')
          .onWrite((data, context) => {  // <= CHANGE
            // Get an object representing the document
             console.log('chat triggered');
            // perform desired operations ...
      
              // See documentation on defining a message payload.
              var message = {
                notification: {
                  title: 'Hello World!',
                  body: 'Hello World!'
                },
                topic: context.params.chatID // <= CHANGE
              };
      
              // Send a message to devices subscribed to the provided topic.
              return admin.messaging().send(message)  // <= CHANGE
                .then((response) => {
                  // Response is a message ID string.
                  console.log('Successfully sent message:', response);
                  return
                })
                .catch((error) => {
                  console.log('Error sending message:', error);
                  return
                });
      
      });
      

      【讨论】:

      • 对我不起作用...我在模拟器中运行这两个功能以及 firestore。
      • @Zorayr,您在发送消息时似乎没有使用令牌或主题。那可能是你的问题。无论哪种方式,我都在向一个主题发送消息,并且我的函数没有触发函数控制台上的任何内容。
      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2021-10-06
      相关资源
      最近更新 更多