我有一个应用程序,用户可以通过 Firebase 注册/登录。用户可以拥有多个设备,在这些设备之间共享他的所有数据(当然他必须登录)。我通过 Firebase 设备令牌跟踪所有设备,并在用户在特定设备上更新某些内容时发送适当的更新通知
现在我知道 firebase 令牌正在刷新,但我如何知道令牌无效?假设一名用户拥有 4 台设备,并使用一个帐户登录。现在,他删除了其中一个应用程序,然后再次安装,因此他获得了一个新令牌。这意味着现在我的服务器上有 5 个设备令牌,但仍然只有 4 个设备。最好的方法是将令牌与某些不可更改的设备 ID(例如 MAC 或 IMEI)绑定,但由于隐私政策,这是不可能的。
是否有其他方法可以捞出已被撤销/失效的代币?
检测过期/撤销的 FCM 令牌的常见方法是在发送消息期间。此时,FCM 将准确告诉您哪些令牌已过期,然后您可以将它们从数据库中删除。
有关此示例,请参阅以下 Node.js 代码:函数样本存储库:
tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
const response = await admin.messaging().sendToDevice(tokens, payload);
// 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') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)