我试图发送个性化的推送通知给所有用户在特定的时间每天。我发送这些文件的方法是:
> Cloud /Sub> Cloud函数> Cloud >推送通知
这对于通用消息很有效,但我想将它们个性化--这意味着我需要访问特定用户的子集合(如users/userID/products/product1 )中的数据。
正如所描述的here一样,无法使用Pub/Sub触发器直接获取用户ID --因此,我要做的是创建第二个函数,该函数将触发要为每个用户创建的通用文档,然后将其用作个性化通知的触发器:
云调度器> Cloud /Sub>第一个函数为每个用户创建文档>第二个函数触发个性化推送通知
我遇到的问题是,我无法解决如何使用一个函数为每个用户创建一个通用文档,我尝试过这样做:
exports.addNotificationsDocumentFunction = functions
.pubsub
.topic('notificationDocuments')
.onPublish((message, context) => {
console.log(message);
console.log('The function was triggered at ', context.timestamp);
console.log('The unique ID for the event is', context.eventId);
var data = {
createdAt : context.timestamp.toString
}
return admin.firestore()
.collection('users')
.doc({userID})
.collection('userNotifications')
.doc('latestNotification')
.set(data)
.catch(error => {
console.log('Error writing document: ' + error);
return false;
});
});我得到以下错误:
ReferenceError: userID is not defined
at exports.addNotificationsDocumentFunction.functions.pubsub.topic.onPublish (/srv/index.js:51:19)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:132:23)
at /worker/worker.js:825:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)是否可以使用Google函数为每个用户创建一个文档?
或者,是否有更好的方法来发送每日推送通知,其中包含来自Firestore的个性化数据?
发布于 2020-10-10 18:24:20
Firestore没有用于同时创建、更新或删除多个文档的通配符。如果您有一个用户文档列表,并且您想为每个用户做一些事情,那么您必须查询该集合,迭代文档,并为每个用户执行工作。所以,
collection
中创建/更新一个新文档
https://stackoverflow.com/questions/64296698
复制相似问题