写入数据库时​​未触发Firebase功能

我的数据库的结构如下Collection(“ Message”)。Document(“ message”)

但实际上,我希望在添加文档时监视数据库主集合中的任何更改。我添加了消息文档,是因为我认为文档是自动生成的,因此可能未调用该函数。但是,问题仍然存在...

仅出于背景原因,我是一名iOS开发人员,所以也许我在这里做错了什么:

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

admin.initializeApp();

exports.sendPushNotifications = functions.database.ref('/Messages/{message}').onCreate((snapshot,context) => {

    console.log(snapshot);
    console.log(context);

    var topic = "/topics/sentMessages";

    var payload = {
        data: {
            message : 'You recieved a new message!'
        }
    }

    return  admin.messaging().sendToTopic(topic,payload).then((response) => {

        return response;
    })
})

有关其他背景:使用控制台时,应用程序可以很好地接收推送通知,无论是直接发送到测试设备还是使用主题。写入Firebase Firestore时,这个问题严格来说是...

评论
  • 凄迷
    凄迷 回复

    When you said "Collection("Message").Document("message")" that suggested to me that you're using Firestore as your database. However, your function is targeting changes to Realtime Database, which is a completely different thing. functions.database builds function for Realtime Database. functions.firestore builds functions for Firestore. You will want to read the documentation on Firetore triggers to learn how to write them.