Firebase 云消息传递 - 管理注册令牌

Firebase Cloud Messaging - Managing Registration Tokens

我正在考虑使用 Firebase 云消息传递在移动和浏览器应用程序之间实现消息传递,我有几个问题,文档似乎没有回答。

为了能够接收消息,您需要注册令牌 (RT)。 消息可以发送到RT,主题或设备组notification_key。RT也可以使用:

转发也可以expire/change.

在我的应用程序中,我维护了每个用户的转发列表。现在,当 RT 改变时:

  1. 我是否必须取消订阅旧令牌并使用新令牌订阅主题?
  2. 我是否必须删除旧令牌并将新令牌添加到设备组?
  3. 是否可以获取有关令牌的设备 groups/topics 的信息?
  4. 我可以多次将令牌添加到设备组吗?
  5. 我可以多次订阅同一个主题的令牌吗?
  6. 多个 subscriptions/additions 相同的令牌是否会导致收到重复的消息

抱歉,问题很多,但我想,对于经历过这个的人来说,应该是馅饼 ;)

  1. Do I have to unsubscribe the old token and subscribe the new token to topics?

AFAIK,您不必取消订阅旧令牌,因为它会被 FCM 本身丢弃。对于新令牌,是的,您必须将其订阅到您需要的主题。通常的事情是通过在 onTokenRefreshed().

中设置 subscribeToTopic() 来完成的(在 Android 中)
  1. Do I have to remove the old token and add the new token to device groups?

是的。您必须为设备组消息处理 mapping/relationships。请参阅我的回答 。这与主题不同。该令牌将失效,但将保留为相应注册密钥的注册令牌列表的一部分。

这就是为什么如果您发送到设备组,其中一个令牌可能会收到 NotRegistred 错误。 :)


  1. Is it possible to get information about device groups/topics for a token?

对于设备组消息(与#2 相同),开发人员(您)必须自己管理这些详细信息。对于主题,可以使用InstanceID API。具体来说,将 details 参数设置为 true:

[optional] boolean details: set this query parameter to true to get available IID token details, including connection information and FCM or GCM topic subscription information (if any) for the device associated with this token. When not specified, defaults to false.


  1. Can I add a token to a device group more than once?

嗯。是的。您是指 same 令牌吗?如果是这样,我还没有尝试过。不妨在添加之前在客户端做一些检查。


  1. Can I subscribe a token to a topic more than once?

如果你的意思是re-subscribing,那么是的。如果你的意思是重复请求订阅,我认为结果仍然是成功的。但是行为没有变化。


  1. Will multiple subscriptions/additions of the same token result in receiving duplicate messages?

测试过了。您不会收到重复主题订阅和将相同令牌添加到设备组的重复消息。如果设备组已经 subscribed/added,FCM 似乎会忽略对 subscribe/add 注册令牌的请求。