Andy
Andy

Reputation: 9

real time database trigger notification

Down below is the trigger for the change in the real time database

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

});

but when i test on my phone it won't trigger while the changing in the database, is there any problem about my code?

Upvotes: 0

Views: 255

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83153

Note that Cloud Functions have been recently updated to V 1.0 with some changes in the syntax, see:

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

If you are using this new version you have to change your code from:

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

to

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

To check which version you are using, look into your package.json file (dependencies node)

Upvotes: 1

Related Questions