我编写了一个云函数,以便在数据库中发生更改但目前不起作用时接收 fcm:
exports.sendPush = functions.database.ref('/news-6ff9e').onWrite(event => {
let projectStateChanged = false;
let projectCreated = false;
let projectData = event.data.val();
if (!event.data.previous.exists()) {
projectCreated = true;
}
if (!projectCreated && event.data.changed()) {
projectStateChanged = true;
}
let msg = 'A project state was changed';
if (projectCreated) {
msg = `The following new project was added to the project: ${projectData.title}`;
}
let payload = {
notification: {
title: 'Firebase Notification',
body: msg,
sound: 'default',
badge: '1'
}
};
return admin.messaging().sendToTopic("/topics/news", payload);
});
/news-6ff9e
是我的项目 ID。
我正在使用/news-6ff9e
作为我的参考,即项目 ID。我已经用我的数据库对象更改了这一点,问题已经解决。