Firebase Cloud Functions for Firestore are not triggering

五迷三道 提交于 2021-02-10 06:34:48

问题


Cannot get Firebase Cloud Functions for Firestore to trigger on onWrite of my collection. 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. Neither is happening. Push is working with other sources.

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"

UPDATED: Updated the code to reflect what we currently have deployed, still not working.


回答1:


There are chances you are using the old syntax (before V1.0) with the new library (v1.0). See the Migration Guide: https://firebase.google.com/docs/functions/beta-v1-diff and check the version in your package.json file.

In addition, note that a Cloud Function must always return a Promise (or if you cannot, at least a value, for asynchronous functions). See this documentation (and associated video) which explain that in detail: 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:

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

});



回答2:


Your firebase-admin initialization syntax, admin.initializeApp(), suggests you are using Cloud Functions SDK version 1.0.x. The parameters for onWrite() have changed in version 1.0.x from earlier versions. You also need to return a Promise for asynchronous action admin.messaging.send(). 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
          });

});


来源:https://stackoverflow.com/questions/50206137/firebase-cloud-functions-for-firestore-are-not-triggering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!