检查Firebase注册令牌是否无效 [英] Check if Firebase registration token is invalid

查看:138
本文介绍了检查Firebase注册令牌是否无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在其中通过Firebase注册/登录.用户可以有多个设备在其中共享他的所有数据(当然,他必须登录).我会通过Firebase设备令牌跟踪所有设备,并在用户更新特定设备上的内容时发送适当的更新通知

I have an app where a user can register/login via Firebase. A user can have multiple devices among which all his data is being shared (of course he must be logged in). I keep track of all devices by their firebase device token and send appropriate update notifications when the user updates something on a particular device

现在我知道Firebase令牌正在刷新,但是我怎么知道令牌无效?假设某个用户有4个设备,使用一个帐户登录该设备.现在,他删除其中一个应用程序,然后再次安装,因此他获得了一个新令牌.这意味着现在我的服务器上有5个设备令牌,但仍然只有4个设备.最好的方法是将令牌绑定到某些不可更改的设备ID(例如MAC oder IMEI),但是由于隐私策略的原因,这是不可能的.还有其他方法可以找出已经撤销/无效的令牌吗?

Now I know that the firebase token is being refreshed, but how do I know that a token is invalid? Lets say a user has 4 devices where he is logged with one account. Now he delete the app on one of them, and installs it again, so he gets a new token. This means that now I have 5 device tokens on my server but still just 4 devices. The best approach would be to tie a token to some non-changeable device id like MAC oder IMEI but because of privacy policies that is not possible. Is there some other way to fish out the tokens that have been revoked/invalidated?

推荐答案

检测过期/已撤销的FCM令牌的常用方法是在消息发送期间.此时,FCM会准确告诉您哪些令牌已过期,然后您可以将其从数据库中删除.

The common way to detect expired/revoked FCM tokens is during sending of messages. FCM will at that point tell you exactly which tokens have expired, and you can then remove them from your database.

有关此示例,请参阅

 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);

这篇关于检查Firebase注册令牌是否无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆