我无法推送 FMC 通知,当我调用 sendTheMessage 函数时,如果我使用下面的代码从 Firestore 检索令牌,但如果我手动设置令牌(
const tokenParam ="myToken"
)并通过,我的设备不会收到通知tokenParam 到我的函数中,我的设备收到通知。我怎样才能做到这一点,以便我可以从 Firestore 获取一个(最终超过一个)令牌,然后使用此类令牌发送消息?
FMC推送在手动传递token时起作用。
exports.checkforsmsnotifs3 = onSchedule("*/5 * * * *", async (event) => {
const db = getFirestore();
const allDocsRef = db.collection("dates");
const tokenParam = "myToken"
// Send notifications .
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);
};
sendTheMessage(tokenParam); //call and pass
});
但在查询 firestore 传递令牌时不推送通知
// Send notifications to all tokens.
const sendTheMessage = async (tokenParam) => {
const mess = {
token: tokenParam,
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.",
},
};
logger.log("func was called");
logger.log("tokenparam: "+ tokenParam);
// await admin.messaging().send(mess);
getMessaging().send(mess).then((res) => {
logger.log("response: ", res);
}).catch((error) => {
console.log("Error sending message:", error);
});
};
for (const d of snapshot.docs) { // try with for loop then for e
// 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
// call the sendTheMessage function
}
} catch (error) {
console.log("error catched in the for loop with:"+error);
}
}
尝试在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();
const snapDocs=await snapshot.docs;
await Promise.all(snapDocs.map(async (d) => {
try {
console.log(d.data().toNumber);
const tokRef = await db.collection("usersByPhoneNum").doc(d.data().toNumber);
const doc = await tokRef.get();
let currentTokenStrin=await doc.data().token +'';
if (!doc.exists) {
logger.log("No such document!");
} else {
logger.log("Document data:", doc.data().token);
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.",
},
};
const options = {
priority: 'high',
timeToLive: 60 * 60 * 12 //notification time to live in seconds :12 hours
};
logger.log("func was called");
logger.log("tokenparam: " + currentTokenStrin);
await admin.messaging().sendToDevice(currentTokenStrin, mess, options)
.then((response) => {
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
} catch (error) {
console.log('error catched in the for loop with:'+error)
}
}))
});