1
我试图在添加新帖子时将通知推送到Android应用程序。但是,无论何时数据被“更改”,即使删除了我不需要的帖子,通知都会到达。我怎样才能把条件,使FCM发送通知只有当帖子被添加。这里是我的index.js文件确定是否将数据添加或删除到Firebase实时数据库
const functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPush = functions.database.ref('/promos').onWrite(event => {
var topic = "deals_notification";
let projectStateChanged = false;
let projectCreated = true;
let projectData = event.data.val();
if (!event.data.previous.exists()) {
// Do things here if project didn't exists before
}
if (projectCreated && event.data.changed()) {
projectStateChanged = true;
}
let msg = "";
if (projectCreated) {
msg = "A project state was changed";
}
if (!event.data.exists()) {
return;
}
let payload = {
notification: {
title: 'Firebase Notification',
body: msg,
sound: 'default',
badge: '1'
}
};
admin.messaging().sendToTopic(topic, payload).then(function(response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
}).catch(function(error) {
console.log("Error sending message:", error);
});
});