简体   繁体   English

Firestore的Firebase Cloud功能未触发

[英]Firebase Cloud Functions for Firestore are not triggering

Cannot get Firebase Cloud Functions for Firestore to trigger on onWrite of my collection. 无法获取Firestore的Firebase Cloud Functions在我的集合的onWrite上触发。 Trying to setup device to device push notification for chat app. 尝试为聊天应用设置设备到设备的推送通知。 Function is deployed and on Pay as you go plan, however, changes in document, updates or create in "chats" collection is not triggering. 功能已部署,并且按计划付款,但是不会触发文档更改,更新或在“聊天”集合中创建。 Firebase cloud messaging is supposed to send a push and write to the log. Firebase云消息传递应该发送推送并写入日志。 Neither is happening. 都没有发生。 Push is working with other sources. Push正在与其他来源合作。

Thanks for your help, wish device to device push notifications was easier, plan is to watch the chat document and fire push notifications on update or create of new conversation. 感谢您的帮助,希望设备到设备的推送通知更加轻松,计划是观看聊天文档并在更新或创建新对话时触发推送通知。 Open to other ideas. 开放其他想法。 Thanks 谢谢

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);
      });

  });

UPDATE: I'm using "firebase-functions": "^1.0.1" 更新:我正在使用“ firebase-functions”:“ ^ 1.0.1”

UPDATED: Updated the code to reflect what we currently have deployed, still not working. 更新:更新了代码以反映我们当前已部署的内容,但仍无法正常工作。

There are chances you are using the old syntax (before V1.0) with the new library (v1.0). 您有可能在新库(v1.0)中使用旧语法(在V1.0之前)。 See the Migration Guide: https://firebase.google.com/docs/functions/beta-v1-diff and check the version in your package.json file. 请参阅《迁移指南》: https : //firebase.google.com/docs/functions/beta-v1-diff并检查package.json文件中的版本。

In addition, note that a Cloud Function must always return a Promise (or if you cannot, at least a value, for asynchronous functions). 此外,请注意,Cloud Function必须始终返回Promise(或者,如果您不能为异步函数返回至少一个值)。 See this documentation (and associated video) which explain that in detail: https://firebase.google.com/docs/functions/terminate-functions 请参阅此文档(以及相关的视频),其中详细说明了此内容: https : //firebase.google.com/docs/functions/terminate-functions

You should modify you code this way: 您应该通过以下方式修改代码:

If you are using Cloud Functions 1.0 or above: 如果您使用的是Cloud Functions 1.0或更高版本:

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

Returning: 返回:

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
      });

});

Your firebase-admin initialization syntax , admin.initializeApp() , suggests you are using Cloud Functions SDK version 1.0.x. 您的admin.initializeApp() firebase-admin 初始化语法 admin.initializeApp()表示您正在使用Cloud Functions SDK1.0.x。 The parameters for onWrite() have changed in version 1.0.x from earlier versions. 1.0.x版中的 onWrite()参数已从早期版本更改 You also need to return a Promise for asynchronous action admin.messaging.send() . 您还需要为异步操作admin.messaging.send() 返回Promise The three needed corrections are noted below: 以下是需要进行的三个更正:

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
          });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM