firebase - firebase云函数如何调用清理函数
问题描述
我在 db 中有通知令牌,超过 10k,我想像这样发送数据消息:
const promises = [];
l = niz.length;
for (i = 0; i < l; i++) {
promises.push(admin.messaging().sendToDevice(niz[i], payload, options));
}
return Promise.all(promises);
在 niz 我有令牌数组,长度为 999。如何从此代码调用清理函数,这将删除无效的令牌。我需要响应和 niz[i] 发送到清理功能。但我不知道怎么做,因为我发送的承诺不止一个......
解决方案
您可以检查每条消息是否有任何错误。
const promises = [];
l = niz.length;
for (i = 0; i < l; i++) {
promises.push(admin.messaging().sendToDevice(niz[i], payload, options));
}
return Promise.all(promises).then((response) => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
// Remove Token : tokens[index]
}
}
});
return Promise.all(tokensToRemove);
});
您可以在FCM 发送通知中找到完整的解决方案
推荐阅读
- exec - 将包含 COALESCE 的动态 sql 语句的结果分配给变量
- python - 从python的内存对象中获取原始数据类型
- java - 未知 NullPointerException 10 忽略>
- statistics - 按时间段过滤的类别加权平均值
- html - @media 查询在 Chrome 或 Safari iOS 中不起作用
- javascript - 如何根据文本而不是类单击按钮
- dll - sqlite3 extension-functions:找不到指定的模块
- python - 为什么我对 QCheckBox.hitButton 的重新实现仅适用于检查指示器的一小部分区域?
- wordpress - 有没有办法让“require_once”条目“出列”?
- database - Golang gocql 无法连接到 Cassandra(使用 Docker)