jac*_*lau 7 firebase firebase-cloud-messaging
AFAIK,将在以下4种情况下刷新Firebase实例令牌:
应用删除实例ID
应用已在新设备上还原
用户卸载/重新安装应用程序
用户清除应用数据
假设用户使用令牌A作为其“ FCM地址”。每次登录该应用程序时,他都会将令牌A与该用户的UUID一起注册到Firestore中,以便可以将特定于用户的云消息发送给他。当他注销时,系统将向Firestore发出删除令牌A记录的请求。
现在,当用户重新安装应用程序时,实例ID会刷新并生成新的令牌B。令牌A变得无用。不幸的是,如果用户在卸载前未注销,则令牌A将永久保留在Firestore中。
有什么解决方法或更明智的方式来处理这种情况?
Fra*_*len 15
使令牌注册表保持最新需要两个步骤:
删除不再使用的令牌的方法是#1。
不过,第二步是在尝试向其发送消息时收到messaging/invalid-registration-token
或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
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
上面的代码将Firebase Admin SDK用于Node.js,但是相同的逻辑也可以应用于其他平台,或者在通过HTTPS端点发送消息时使用。
归档时间: |
|
查看次数: |
1607 次 |
最近记录: |