node.js - 实时数据库功能不断触发
问题描述
我已经为 Firebase 实时数据库触发器部署了一个 JS 函数。在其操作中,它应该仅在数据库中的值更新时发送推送通知,这非常简单:
{
"rollo" : "yes"
}
如果值更改为 yes,则应触发通知。如果它变成“否”,那么它应该什么都不做。这是JS函数:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNewPostNotif = functions.database.ref('/rollo').onUpdate((change, context) => {
console.log('Push notification event triggered');
const beforeData = change.before.val();
const payload = {
notification: {
title: 'Push triggered!',
body: "Push text",
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60 * 10 * 1
};
return admin.messaging().sendToTopic("notifications", payload, options);
});
此外,即使我设置了 TTL,每次值更改都会发送另一个推送通知。
有任何想法吗?
解决方案
我会尝试这样的事情:
exports.sendNewPostNotif = functions.database.ref('/rollo').onWrite((change, context) => {
const newData = change.after.val();
const oldData = change.before.val();
const payload = {
notification: {
title: 'Push triggered!',
body: "Push text",
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60 * 10 * 1
};
if (newData != oldData && newData == 'yes') {
return admin.messaging().sendToTopic("notifications", payload, options);
}
});
推荐阅读
- javascript - 看起来像猫鼬查询的 Promise.all() 与顺序等待一样花费时间
- aggregate-functions - 聚合函数在 AgnsGraph 上不起作用
- python - 为什么在 python 的 send 方法中我们需要“启动”一个生成器,而在普通方法中我们不需要这样做?
- java - 在写入文件之前删除前缀
- c# - 从 C# 桌面应用程序中的 json 字符串响应中获取数据
- elasticsearch - Elasticsearch ctx 将 json 对象附加到数组
- azureportal - Azure 成本管理访问之谜
- vue.js - 切换列表以根据选定的单选按钮显示
- tensorflow - 如何从视频数据集制作数据集(首先是tensorflow)
- c++ - 为什么我的控制台程序可以连接g29车轮,但无法读取车轮数据?