我正在关注 Udacity 的本教程,其中 Cloud Functions for Firebase 用于更改添加数据的参考数据。我想使用类似的功能,但用于向订阅主题的用户发送推送通知。
这是我正在使用的功能。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/messages/{pushId}/text').onWrite((event) => {
const data = event.data;
console.log('Message received');
if(!data.changed()){
console.log('Nothing changed');
return;
}else{
console.log(data.val());
}
const payLoad = {
notification:{
title: 'Message received',
body: 'You received a new message',
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60*60*2
};
return admin.messaging().sendToTopic("Message_Notifications", payLoad, options);
});
查看 Firebase 函数日志,我可以看到当我向数据库添加新值时调用了该函数。我正在订阅 mainactivity 中的主题
FirebaseMessaging.getInstance().subscribeToTopic("Message_Notification");
几个小时后,我可以在 firebase 通知控制台的主题列表中看到该主题。我从那里发送通知并且它有效。
每次添加新消息时,我还需要做哪些其他事情才能显示通知?
任何建议表示赞赏。我对 Firebase 相当陌生。因此,如果有更好的博客/帖子,请重定向我,以便我可以了解更多信息。
提前致谢。