【发布时间】:2019-04-02 07:43:53
【问题描述】:
我看到的大多数示例都解释了如何使用用户的 uid 收听文档。我正在尝试只收听一般父集合
exports.sendNotifications = functions.firestore.document('Notifications').onCreate(async (snapshot) => {
// Notification details.
const payload = {
notification: {
title: 'Hello',
body: 'Hello again',
click_action: `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`,
}
};
// Get the list of device tokens.
const allTokens = await admin.firestore().collection('fcmTokens').get();
const tokens = [];
allTokens.forEach((tokenDoc) => {
tokens.push(tokenDoc.id);
});
if (tokens.length > 0) {
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokens, payload);
}
});
此云功能带来functions: failed to create function sendNotifications
HTTP Error: 400, The request has errors。我猜的错误是因为没有正确引用firestore集合。它是根集合。我怎样才能更好地参考它
【问题讨论】:
-
不清楚你说的是哪个系列。您能否添加数据模型的详细信息,并提供有关何时触发 Cloud Function 的更多信息(在
Notifications集合中创建文档时? -
我需要在添加文档时收听
Notifications集合,然后向我拥有令牌的用户发送 fcm 通知。 -
通知(根集合)-> 文档和 fcmTokens(根集合)-> 文档。数据模型就是这么简单
标签: javascript firebase google-cloud-firestore google-cloud-functions