由于某种原因,当我调用
sendTheMessage
函数时,如果我从 Firestore 检索令牌,我的设备(而不是模拟器)不会收到通知。但是,当我取消注释 const tok = ... 部分(手动设置令牌并将 tok 传递到我的函数中)时,我的设备(而不是模拟器)收到通知。我怎样才能做到这一点,以便我可以从 Firestore 获取一个(最终超过一个)令牌,然后使用此类令牌发送消息?
const admin = require("firebase-admin");
const { onSchedule } = require("firebase-functions/v2/scheduler");
const { logger } = require("firebase-functions");
const { getFirestore } = require("firebase-admin/firestore");
admin.initializeApp();
exports.checkforsmsnotifs3 = onSchedule("*/5 * * * *", async (event) => {
const db = getFirestore();
const allDocsRef = db.collection("dates");
const snapshot = await allDocsRef.where("checkedIn", "==", false).get();
// TODO: get the token for the correct user
// const tok = "fUPMs-9eB0MTgW6V6mzzqf:APA91bF9a2iSaa_"+
// "d4xECQIYT6HXprjY1ZBP6Bj8Xt0tH12GH3Pp1jQai-Qrbqxowp"+
// "-1UDe6nvUUfWO8CSm10xO1G7n"+
// "ppsCnPGFYO4cKlg9FehscrJc84cNUTqSz1oetPKNVneWnFOtVJ";
// Send notifications to all tokens.
const sendTheMessage = async (tokenParam) => {
const mess = {
notification: {
title: "Your friend is out on a date",
body: "Your friend was supposed to check in on " +
"CheckDate but hasn't yet." +
" You may want to reach out.",
},
token: tokenParam,
};
logger.log("func was called");
logger.log("tokenparam: " + tokenParam);
await admin.messaging().send(mess);
};
snapshot.forEach(async (d) => {
console.log(d.data().toNumber);
const tokRef = db.collection("usersByPhoneNum").doc(d.data().toNumber);
const doc = await tokRef.get();
if (!doc.exists) {
logger.log("No such document!");
} else {
logger.log("Document data:", doc.data().token);
sendTheMessage(doc.data().token);
}
});
});
尝试在sendTheMessage之前添加await。我怀疑这与异步有关,但一直无法破解。我知道该函数正在被调用,因为我看到了日志。但我的设备上没有通知。
尝试以下方法
exports.checkforsmsnotifs3 = onSchedule("*/5 * * * *", async (event) => {
const db = await getFirestore();
const allDocsRef =await db.collection("dates");
const snapshot = await allDocsRef.where("checkedIn", "==", false).get();
// TODO: get the token for the correct user
// const tok = "fUPMs-9eB0MTgW6V6mzzqf:APA91bF9a2iSaa_"+
// "d4xECQIYT6HXprjY1ZBP6Bj8Xt0tH12GH3Pp1jQai-Qrbqxowp"+
// "-1UDe6nvUUfWO8CSm10xO1G7n"+
// "ppsCnPGFYO4cKlg9FehscrJc84cNUTqSz1oetPKNVneWnFOtVJ";
// Send notifications to all tokens.
const sendTheMessage = async (tokenParam :any) => {
const mess = {
notification: {
title: "Your friend is out on a date",
body: "Your friend was supposed to check in on " +
"CheckDate but hasn't yet." +
" You may want to reach out.",
},
token: tokenParam,
};
logger.log("func was called");
logger.log("tokenparam: " + tokenParam);
await admin.messaging().send(mess);
};
for (const d of snapshot.docs) { //try with for loop then for each also use await as shown
try {
console.log(d.data().toNumber);
const tokRef = await db.collection("usersByPhoneNum").doc(d.data().toNumber);
const doc = await tokRef.get();
if (!doc.exists) {
logger.log("No such document!");
} else {
logger.log("Document data:", doc.data().token);
await sendTheMessage(doc.data().token); // use await the sendTheMessage function
}
} catch (error) {
console.log('error catched in the for loop with:'+error)
}
}
// snapshot.forEach(async (d) => {
// try {
// console.log(d.data().toNumber);
// const tokRef = await db.collection("usersByPhoneNum").doc(d.data().toNumber);
// const doc = await tokRef.get();
// if (!doc.exists) {
// logger.log("No such document!");
// } else {
// logger.log("Document data:", doc.data().token);
// sendTheMessage(doc.data().token);
// }
// } catch (error) {
// console.log('catch an error:'+error)
// }
// });
});
请告诉我。