我对在我的应用中实施 Firebase 云消息传递有些疑问.
I have some doubts about implementing Firebase Cloud Messaging in my app.
我的应用是一个类似于 whatsapp 和 facebook messenger 的聊天应用.现在每次有人登录时,我都会注册令牌并将其与用户相关联.因此,如果有人想与用户聊天,应用程序会在 Db 中搜索用户 ID,而我有他的令牌.
My app is a chat similar to whatsapp and facebook 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.
我可以注册设备令牌,但我不确定何时应该这样做.
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.
例如:用户 Frehley 使用 Galaxy 8 并且能够使用令牌在他的设备中聊天和接收通知.但是可以说他登录了另一个设备.我需要重新注册令牌并将其与他关联对吗?
For example: User Frehley uses a Galaxy 8 and is able to chat and receive notifications in his device using the token. 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. Right?
现在,假设两个用户(Frehley 和 Stanley)使用相同的 Galaxy 8 登录,每次登录时令牌都会不同,还是每个设备都相同?
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?
这一切都取决于用例:
如果同一用户登录到两台设备,您是否希望他们在两台设备上都收到通知?如果是这样,您需要为每个用户存储多个设备令牌并将通知发送到目标用户的每个令牌.您还可以使用设备组来识别单个用户的设备一个>.从这些文档中:
借助设备群组消息,您可以向在属于某个群组的设备上运行的应用程序的多个实例发送一条消息.通常,组"是指属于单个用户的一组不同设备
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
如果您希望用户仅在他们上次使用的设备上收到通知,则您必须将单个令牌与每个用户相关联,并在他们在设备上启动应用程序时覆盖该令牌.并不是说这不一定是在他们登录时,而是在应用 检测到它们已通过身份验证.
如果不同的用户在同一设备上使用同一应用(使用相同的设备配置文件),您可能希望停止向设备令牌发送.最可靠的方法是删除前一个用户和令牌之间的关联,但这可能需要您保留从令牌到 UID 的映射.更简单的方法是删除设备令牌 当用户退出时.然后新用户登录,他们将获得一个新的令牌.
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. 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.
这篇关于何时为用户注册 FCM 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!