处理 FCM 设备组的刷新令牌 [英] Handling refresh tokens for FCM device groups

查看:14
本文介绍了处理 FCM 设备组的刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Node.js 服务器在我的 Android 应用程序中实现 Firebase 云消息传递,但我遇到了一个用例.

I am trying to implement Firebase cloud messaging in my Android app through a Node.js server and I have got stuck at a usecase.

我看到了使用注册令牌创建设备组以向同一用户登录的所有设备发送消息/通知的 Firebase 教程,我不明白当其中一个注册令牌被 onTokenRefresh 刷新时会发生什么() 方法.

I saw the Firebase tutorial of creating a device group using registration tokens to send messages/notifications to all devices with the same user logged in, what I don't understand is what happens when one of the registration tokens get refreshed by onTokenRefresh() method.

我将如何区分要更改的令牌,因为所有令牌都属于同一用户?

How will I distinguish which token to change as all will be belonging to the same user?

更新:

好的,所以现在我陷入了另一个阻塞用例.我正在创建一个由我的服务器中的用户 ID 标识的用户组.如果用户立即卸载并重新安装应用程序并且另一个用户在设备上登录,如果我在以前的用户组上调用 gcm 消息,该设备仍然会收到它.

Ok, so now I have got stucked on another blocking use case. I am creating a user group identified by the user id from my server. If user uninstalls and reinstalls the app immediately and another user logs in on the device, if I call a gcm message on the previous user group this device still receives it.

gcm 有什么方法可以识别它发送通知的设备是否已登录,如果已登录,它是否使用与组相同的用户登录?

Is there any way for the gcm to identify is the device it is sending the notification to is logged in or not and if it is, is it logged in with the same user as for the group?

推荐答案

所以我一直在思考如何处理这个场景.首先,让我们放入 的实例onRefreshToken() 被调用:

So I've been thinking about how to go with this scenario. First off, let's put in the instances when onRefreshToken() is called:

这不会被非常频繁地调用,它是密钥轮换和处理实例 ID 更改所必需的,原因如下:

This will not be called very frequently, it is needed for key rotation and to handle Instance ID changes due to:

  • 应用删除实例 ID
  • 应用已在新设备上恢复
  • 用户卸载/重新安装应用
  • 用户清除应用数据

你猜,你可以说'onTokenRefresh()`将在上述情况发生并且设备在线后被调用(当然它必须在线才能获得新令牌).所以我想这就是我将如何处理这个场景:

Guess with that, you can say that 'onTokenRefresh()` will be called after one the above happens and if the device is online (of course it has to be online on order to get a new token). So I guess here's how I'd go on the scenario:

首先,在注册时,我会保存注册令牌并将其与另一个标识符配对,比如说一个 deviceId(因为我们处于一个用户拥有多个设备的场景)应用服务器.

First off, upon registration, I would save the registration token and pair it along another identifier, let's say a deviceId (since we're in a scenario for a user with multiple devices) in my App Server.

假设我添加了 3 个注册令牌,它们也与它们的 deviceId 配对.我将它们全部添加到设备组中.

So assume I add in 3 registration tokens, those are also paired with their deviceIds. I add them all to a device group.

现在说其中一个设备触发了 onTokenRefresh(),我会立即向我的应用服务器发送一个删除请求,以获取当前与该 deviceId 配对的注册令牌(您也应该删除它在它连接到的任何设备组中),将其替换为新设备组,然后将其重新添加到相应的设备组中.

Now say one of the devices triggers the onTokenRefresh(), I would immediately send a delete request to my App Server for the registration token that is currently paired to that deviceId (you should also delete it in any device group(s) it's connected to), replacing it with the new one, then re-add it to the corresponding device group(s).

这是我能想到的最简单的方法.这里的关键是让您将注册令牌与另一个标识符配对,并使用它来查找您需要替换的注册令牌.

That's the simplest way I can think of. The key here is for you to pair the registration tokens with another identifier and use it to find which registration token you need to replace.

这篇关于处理 FCM 设备组的刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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