使用主题发送推送通知的Firebase云功能



每当在数据库的comments树中的特定meetupId下添加新注释时,我都会尝试触发推送通知。树的结构如下:

comments:
(meetupId)
(commentId)
-createdBy: String
-meetupId: String
-timestamp: Int
-commentText: String

在一个meetup上添加的第一个注释在comments下创建一个子项,该meetup的所有其他注释都添加在该meetupId子项下。我使用meetupId作为这个云函数的主题。因此,每当有评论添加到特定用户订阅的特定meetupId时,我都希望触发推送通知。我是一名Swift开发人员,对JavaScript完全不熟悉,所以这对我来说有点困难,我正在寻找一些写这个云函数的帮助。

这是我迄今为止的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.meetupNotification = functions.database
.ref('/comments/{meetupId}')
.onCreate((snapshot, context) => {
var sender = snapshot.val().createdBy;
var content = snapshot.val().commentText;
var meetupId = snapshot.val().meetupId;
var topic = meetupId;
// Notification details and Payload.
const payload = {
notification: {
title: `A new comment was added to a ${topic} you're attending!`,
body: content,
badge: badgeCount.toString(),
sound: 'default'
},
data: {
statusCode: '101',
senderID: sender,
meetupID: meetupId,
content: content,
}
};
const options = {
priority: 'high',
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic(topic, payload, options);
console.log('Comment notification for  sent successfully')
});

我们非常感谢任何指导和协助。提前谢谢。

为了将来任何可能遇到类似问题的人的参考,我只需将{commentId}添加到ref路径的末尾,就可以使该函数正常工作。所以总的来说,它看起来像这个

exports.meetupNotification = functions.database
.ref('/comments/{meetupId}/{commentId}')

相关内容

  • 没有找到相关文章

最新更新