javascript - Firestore 的 Firebase Cloud Functions 未触发

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

无法让 Firestore 的 Firebase Cloud Functions 在我的集合的 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”

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

最佳答案

您有可能在新库 (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
      });

});

关于javascript - Firestore 的 Firebase Cloud Functions 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50206137/

相关文章:

jquery - 火力地堡错误 : Messaging: We are unable to register the default service worker

javascript - model.set 不会保留backbone.js 中的值

javascript - 网页按价格排序下拉菜单不起作用

Firebase Firestore : How to monitor read document count by collection?

javascript - Firebase 数据库删除所有等于特定值的节点

ios - 启动时恢复简历以及在ios中不起作用的消息上出现Flutter FCM,启动时会触发缺少插件的实现

javascript - 如何通过单击label标签将光标移动到输入文本框中?

javascript - 按照与 Ajax 请求相同的顺序处理 Ajax 响应

javascript - 如何将文件上传到 firebase 存储并显示下载 URL?

android - 如何在下层库中初始化Firebase Cloud Messaging?