Firestore的Firebase Cloud功能未触发 [英] Firebase Cloud Functions for Firestore are not triggering

本文介绍了Firestore的Firebase Cloud功能未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法获取Firestore的Firebase云功能,以在我的收藏集的onWrite上触发.尝试为聊天应用设置设备到设备的推送通知.已部署功能并按计划付款,但是不会触发文档更改,更新或在聊天"集合中创建.Firebase云消息传递应该发送推送并写入日志.都没有发生.Push正在与其他来源合作.

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

  });

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

UPDATE: I'm using "firebase-functions": "^1.0.1"

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

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

推荐答案

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

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.

此外,请注意,Cloud Function必须始终返回Promise(或者,对于异步函数,如果不能,至少返回一个值).请参阅此文档(以及相关的视频),其中详细说明了此内容: https://firebase.google.com/docs/functions/terminate-functions

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:

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

If you are using Cloud Functions 1.0 or above:

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

});

这篇关于Firestore的Firebase Cloud功能未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆