【发布时间】:2020-04-01 08:25:23
【问题描述】:
我想用 node.js 创建一个函数,但我遇到了困难。
说明我想做什么:
首先,该函数将在路径profile/{profileID}/posts/{newDocument}添加新文档时触发
该功能将向以下所有用户发送通知。问题来了。
我在个人资料集合中有另一个集合,它是关注者,包含字段 followerID 的文档。
我想使用这个 followerID 并将其用作文档 ID 来访问我已添加到个人资料文档中的 tokenID 字段。
像这样:
..(profile/followerID).get();然后访问tokenID字段的字段值。
我当前的代码:- Index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.fcmTester = functions.firestore.document('profile/{profileID}/posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
var x = firestore.doc('profile/{profileID}/followers/');
var follower;
x.get().then(snapshot => {
follower = snapshot.followerID;
});
return admin.firestore().collection('profile').get()
.then(snapshot => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
throw new Error('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": notificationMessageData.title,
"body": notificationMessageData.title,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.title,
"message": notificationMessageData.title
}
}
return admin.messaging().sendToDevice(tokens, payload)
}
})
.catch((err) => {
console.log(err);
return null;
})
});
我的 Firestore 数据库说明。
简介 | profile文件|帖子和关注者 |追随者收集文件和帖子收集文件
我有一个名为 profile 的父集合,它包含文档作为任何集合,这些文档包含一个名为 tokenID 的字段并且我想访问,但我不会仅为关注者(关注该配置文件的用户)对所有用户执行此操作) 所以我创建了一个名为 follower 的新集合,它包含所有关注者 ID,我想获取每个 followerID 并为每个 id 推送 tokenID 到令牌列表。
【问题讨论】:
标签: node.js firebase push-notification google-cloud-firestore google-cloud-functions