简体   繁体   English

何时为用户注册FCM令牌

[英]When to register an FCM token for a user

I have some doubts about implementing Firebase Cloud Messaging in my app. 我对在我的应用中实施Firebase云消息传递有一些疑问。

My app is a chat similar to whatsapp and facebook messenger . 我的应用程序是类似于whatsappfacebook messenger的聊天。 Right now every time someone logs in I register the token and relate it to the user. 现在,每当有人登录时,我都会注册令牌并将其与用户联系起来。 So if someone wants to chat to an user the app searches the Db for the user ID and there I have his token. 因此,如果有人想要与用户聊天,应用程序会在Db中搜索用户ID,并且我有他的令牌。

I'm able to register the device token but I'm not sure when should I do that. 我可以注册设备令牌,但我不确定我该怎么做。

In my chat users register through a mail/password signup and is possible that an user could login in other device. 在我的聊天中,用户通过邮件/密码注册进行注册,并且用户可以在其他设备中登录。

For example: User Frehley uses a Galaxy 8 and is able to chat and receive notifications in his device using the token. 例如:用户Frehley使用Galaxy 8 ,并且能够使用令牌在他的设备中聊天和接收通知。 But lets say that he loges in another device. 但是,让我说他在另一台设备上。 I need to register the token again and relate it to him right? 我需要再次注册令牌并将其与他联系起来吗?

So, maybe I'm wrong but the best moment to register the token is every time the user logs in and replace it in the DB. 所以,也许我错了,但每次用户登录并在DB中替换它时,注册令牌的最佳时刻。 Right? 对?

Now, lets say two users (Frehley and Stanley) uses the same Galaxy 8 to login, the token will be diferent everytime the login or it is the same for every single device? 现在,假设有两个用户(Frehley和Stanley)使用相同的Galaxy 8登录,每次登录时令牌都会不同,或者每个设备的令牌都相同?

This all depends on the use-case: 这一切都取决于用例:

  • If the same user is logged in to two devices, do you want them to receive the notification on both devices? 如果同一用户登录到两台设备,您是否希望它们在两台设备上接收通知? If so, you'll need to store multiple device tokens per user and send the notification to each token for the targeted user. 如果是这样,您将需要为每个用户存储多个设备令牌,并将通知发送给目标用户的每个令牌。 You could also use a device group to identify the devices for a single user . 您还可以使用设备组来识别单个用户的设备 From those docs: 从那些文档:

    With device group messaging, you can send a single message to multiple instances of an app running on devices belonging to a group. 使用设备组消息传递,您可以将单个消息发送到在属于组的设备上运行的应用程序的多个实例。 Typically, "group" refers a set of different devices that belong to a single user 通常,“组”是指属于单个用户的一组不同设备

  • If you want the user to only receive the notification on the device that they were last using, you'll have to associate a single token with each user and overwrite that when they start the app on a device. 如果您希望用户仅在设备上接收上次使用的通知,则必须将单个令牌与每个用户关联,并在设备上启动应用时覆盖该令牌。 Not that this is not necessarily when they log in, but more likely when the app detects that they're authenticated . 并不是说这不一定是他们登录时,但更有可能是当应用程序检测到他们已经过身份验证时

  • If a different user uses the same app on the same device (using the same device profile), you probably want to stop sending to the device token. 如果其他用户在同一设备上使用相同的应用程序(使用相同的设备配置文件),您可能希望停止发送到设备令牌。 The most robust way to do this is to remove the association between the previous user and the token, but that may require that you keep a mapping from token to UID. 最强大的方法是删除先前用户和令牌之间的关联,但这可能需要您保持从令牌到UID的映射。 An easier way is to delete the device token when a user signs out. 更简单的方法是在用户注销时删除设备令牌 Then the new user signs in, they will then get a new token. 然后新用户登录,然后他们将获得一个新令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM