【发布时间】:2020-10-15 16:49:09
【问题描述】:
我正在测试将物理 (android) 设备转为飞行模式(无 wifi)的情况。当我将设备恢复在线时,我想接收所有通知。
目前我只收到多个通知之一。
我搜索了有关此案例的文档 Setting the lifespan of a message 并找到了 time_to_live 参数。通过这个参数,我可以编辑通知的生命周期,但我没有使用它,因为默认情况下它设置为 4 周。就我而言,几秒钟/几分钟后,我将设备转为在线。
以下函数在可调用的 Cloud Function 中实现,该函数在设备在线时触发并正常工作。
这是我发送消息的方式:
async function notifyParticipantForRegistrationStatus(
data: any,
participantDeleted: boolean
) {
const uid = data.uid;
const courseId = data.courseId;
const courseEventSnapshot = await firestore
.collection("CourseEvents")
.doc(courseId)
.get();
const courseEvent = courseEventSnapshot.data() as CourseEvent;
// get tokens for this participant
const registrationTokens: string[] = [];
await firestore
.collection("Users")
.doc(uid)
.collection("Tokens")
.get()
.then((querySnapshot) => {
querySnapshot.forEach(function (doc) {
const tokenObj = doc.data() as Token;
registrationTokens.push(tokenObj.token);
});
})
.catch((error) => {
console.error(`registrationTokens: ${registrationTokens}`);
console.error(error);
});
// transform date from utc to german
const d = utcSecondsToGermanDate(courseEvent.start.seconds);
// send notification to the deletedParticipant
if (registrationTokens.length > 0) {
const message = {
notification: {
title: `Kurs ${courseEvent.courseEventTitle}`,
body: participantDeleted
? `Du wurdest vom Kurs ${courseEvent.courseEventTitle} an den ${d.getDate()}.${
d.getMonth() + 1 //getMonth() starts from 0
}.${d.getFullYear()} um ${d.getHours()}:${d.getMinutes()} entfernt. Deine Einheit wurde storniert.`
: `Du wurdest zum Kurs ${courseEvent.courseEventTitle} an den ${d.getDate()}.${
d.getMonth() + 1 //getMonth() starts from 0
}.${d.getFullYear()} um ${d.getHours()}:${d.getMinutes()} registriert.`,
},
tokens: registrationTokens,
} as admin.messaging.MulticastMessage;
fcm
.sendMulticast(message)
.then((response) => {
if (response.failureCount > 0) {
const failedTokens: string | string[] = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(registrationTokens[idx]);
}
});
console.log("List of tokens that caused failures: " + failedTokens);
}
})
.catch((error) => {
console.error(error);
});
}
}
【问题讨论】:
标签: typescript google-cloud-functions firebase-cloud-messaging