AFAIK,Firebase实例令牌将在以下4种情况下刷新:
应用程序删除实例ID
应用程序已在新设备上恢复
用户卸载/重新安装应用程序
用户清除应用数据
假设用户使用令牌 A 作为他的“FCM 地址”。每次登录应用程序时,他都会将令牌 A 以及该用户的 UUID 注册到 Firestore,以便可以向他发送特定于用户的云消息。当他注销时,系统将向 firestore 发出删除令牌 A 记录的请求。
现在,当用户重新安装应用程序时,实例 ID 会刷新并生成新的令牌 B。令牌 A 变得毫无用处。不幸的是,如果用户在卸载之前没有注销,令牌 A 将永远保留在 firestore 中。
有什么解决方法或更明智的方法来处理这种情况吗?
保持令牌注册表最新需要两个步骤:
- 从应用程序代码中删除过时的令牌。
- 检查是否有过时的令牌,并在您使用时将其删除send消息。
删除不再使用的令牌的方法是#1。
第二步是当您获得令牌时从注册表/数据库中删除令牌messaging/invalid-registration-token
or messaging/registration-token-not-registered
尝试向其发送消息时响应。这函数样本存储库包含一个很好的例子:
admin.messaging().sendToDevice(tokens, payload).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') {
// TODO: remove the token from your registry/database
}
}
});
});
上面的代码使用适用于 Node.js 的 Firebase Admin SDK,但相同的逻辑也可以应用于其他平台或通过 HTTPS 端点发送消息时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)