FCM 基于我使用 firebase 查询获得的所有文档(FCM 使用 Cloud 函数)
FCM based all documents which I get using firebase query (FCM using Cloud function)
我有一个 firebase 查询,它给出了来自集合的过滤文档列表,例如。 20 份文件。我能够发送带有负载和主题的文档的 FCM onCreate。
但是现在有 20 个文档,我必须使用循环来发送 FCM,并且每个通知都有基于文档的不同负载。
我不知道如何使用for循环来使用云功能发送通知。
查询示例:
const collectionRef = database.collection(booking);
const query = collectionRef.where('status', '==', 'booked')
.where('startTimeStamp', '>=' , admin.firestore.Timestamp.now())
假设你的函数是async
,你可以做
const docs = (await query.get()).docs;
for (const doc of docs) {
await whatYouDoForOneDoc(doc);
}
Thanks, the answer helped me. Below is the whole cloud function which is currently running fine for me.
exports.givenMinutesNotification = functions.pubsub.schedule('55 23 * * *').onRun(async context => {
const collectionRef = database.collection(Notebook);
const date = new Date(Date.now() + 120*60*1000);
const timestamp = admin.firestore.Timestamp.fromDate(date)
const query = collectionRef.where('status', '==', 'Booked')
.where('startTimeStamp', '>=' , timestamp);
const docs = (await query.get()).docs;
for (const doc of docs) {
whatYouDoForOneDoc(doc);
}
});
async function whatYouDoForOneDoc(doc)
{
let title = "xxxxx";
const docID = doc.id;
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let topic = 'xxxxx';
console.log("-----------------docID<<<<<-----------"+ docID);
var message = {
data:
{
"id": docID,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"status": Booked,
"topic": topic,
},
notification: {
title: title,
body: xxxxx,
},
topic: topic,
android: {
notification: {
clickAction: 'com.xxxxx.xxxxx."ActivityName"',
},
}
};
let response = await admin.messaging().send(message);
console.log(response);
}
我有一个 firebase 查询,它给出了来自集合的过滤文档列表,例如。 20 份文件。我能够发送带有负载和主题的文档的 FCM onCreate。
但是现在有 20 个文档,我必须使用循环来发送 FCM,并且每个通知都有基于文档的不同负载。
我不知道如何使用for循环来使用云功能发送通知。
查询示例:
const collectionRef = database.collection(booking);
const query = collectionRef.where('status', '==', 'booked')
.where('startTimeStamp', '>=' , admin.firestore.Timestamp.now())
假设你的函数是async
,你可以做
const docs = (await query.get()).docs;
for (const doc of docs) {
await whatYouDoForOneDoc(doc);
}
Thanks, the answer helped me. Below is the whole cloud function which is currently running fine for me.
exports.givenMinutesNotification = functions.pubsub.schedule('55 23 * * *').onRun(async context => {
const collectionRef = database.collection(Notebook);
const date = new Date(Date.now() + 120*60*1000);
const timestamp = admin.firestore.Timestamp.fromDate(date)
const query = collectionRef.where('status', '==', 'Booked')
.where('startTimeStamp', '>=' , timestamp);
const docs = (await query.get()).docs;
for (const doc of docs) {
whatYouDoForOneDoc(doc);
}
});
async function whatYouDoForOneDoc(doc)
{
let title = "xxxxx";
const docID = doc.id;
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let xxxxx = doc.get("xxxxx");
let topic = 'xxxxx';
console.log("-----------------docID<<<<<-----------"+ docID);
var message = {
data:
{
"id": docID,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"xxxxx": xxxxx,
"status": Booked,
"topic": topic,
},
notification: {
title: title,
body: xxxxx,
},
topic: topic,
android: {
notification: {
clickAction: 'com.xxxxx.xxxxx."ActivityName"',
},
}
};
let response = await admin.messaging().send(message);
console.log(response);
}