实时数据库触发通知

时间:2018-05-02 13:58:54

标签: node.js firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions

下面是实时数据库更改的触发器

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.notification = functions.database.ref('/chatroom-98902/').onWrite(event => {

   var topic = 'Warning';

   var message ={
       data :{
           title:"Warning",
           body:"123"
        },
       topic: topic
    };
    admin.messaging().send(message)
    .then(function(response) {
        console.log("Successfully sent message:",response);
    })
    .catch(function(error) {
        console.log("Error sending message:",error);
    });

});

但是当我在手机上测试时,在数据库中更改时不会触发, 我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

请注意,云函数最近已更新为V 1.0,语法中有一些更改,请参阅:

https://firebase.google.com/docs/functions/beta-v1-diff

如果您使用的是此新版本,则必须更改以下代码:

exports.notification = functions.database.ref('/chatroom-98902/').onWrite(event => {

exports.notification = functions.database.ref('/chatroom-98902/').onWrite((change, context) => {

要查看您使用的是哪个版本,请查看您的package.json文件(dependencies节点)

相关问题