Firebase云消息传递-管理注册令牌 [英] Firebase Cloud Messaging - Managing Registration Tokens

查看:82
本文介绍了Firebase云消息传递-管理注册令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用Firebase云消息传递在移动应用程序和浏览器应用程序之间实现消息传递,我有一些问题,这些文档似乎无法回答.

I'm looking at implementing messaging between mobile and browser apps using Firebase cloud messaging and i have a few questions, that the docs don't seem to answer.

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

For being able to receive messages, you need a Registration Token (RT). Messages can be send to a RT, to a topic or to a device group notification_key.The RT can also be used:

  • 直接
  • 订阅主题
  • 添加到设备组

RT也可以到期/更改.

The RT can also expire/change.

在我的应用中,我维护着每个用户的RT列表.现在,当RT改变时:

In my app, I'm maintaining a list of RT per user. Now, when the RT changes:

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

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

Sorry, that's a lot of questions, but I guess, for somebody who has gone through this, it should be pie ;)

推荐答案

  1. 我是否必须取消订阅旧令牌并将新令牌订阅到主题?

AFAIK,您不必取消订阅旧令牌,因为它将被FCM本身丢弃.对于新令牌,是的,您必须将其订阅所需的主题.通过在onTokenRefreshed()中使用subscribeToTopic()来完成通常的操作(在Android中).

AFAIK, you don't have to unsubscribe the old token, since it will be discarded by FCM itself. For the new token, yes, you'll have to subscribe it to the topic you need. The usual thing is done (in Android) by having subscribeToTopic() in onTokenRefreshed().

  1. 我是否必须删除旧令牌并将新令牌添加到设备组?

是的.您必须处理设备组消息传递的映射/关系.在此处看到我的答案.这与主题不同.该令牌将失效,但将保留为相应注册密钥的注册令牌列表的一部分.

Yes. You have to handle the mapping/relationships for Device Group Messaging. See my answer here. This is different from topics. The token will be invalidated, but will be kept as part of the list of registration tokens for the corresponding registration key.

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

It's why there's a possibility to receive a NotRegistred error on one of the tokens if you send to Device Group. :)

  1. 是否可以获取有关令牌的设备组/主题的信息?

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

For Device Group Messaging (same with #2), the developer (you) have to manage these details yourself. For topics, you can use the InstanceID API. Specifically, set details parameter to true:

[可选]布尔值details:将此查询参数设置为true,以获取可用的IID令牌详细信息,包括与此令牌关联的设备的连接信息以及FCM或GCM主题订阅信息(如果有).未指定时,默认为false.

[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. 我可以多次向设备组添加令牌吗?

嗯.是的.您是说 same 令牌吗?如果是这样,我还没有尝试过.在添加之前,还可以在客户端进行一些检查.

Ahmm. Yes. Do you mean the same token? If so, I haven't tried it yet. Might as well do some checking on the client side before adding.

  1. 我可以多次向一个主题订阅令牌吗?

如果您打算重新订阅,那就可以.如果您的意思是重复请求订阅,我认为结果仍然会成功.行为没有变化.

If you mean re-subscribing, then yes. If you mean duplicate request to subscribe, I think the result would still be a success. No changes in behavior though.

  1. 同一令牌的多个订阅/添加会导致接收重复消息吗?

进行了测试.您不会收到重复的主题订阅和向设备组添加相同令牌的重复消息.如果FCM似乎已经订阅/添加到设备组,则FCM似乎忽略了订阅/添加注册令牌的请求.

Tested it out. You won't receive duplicate messages for both duplicate topic subscriptions and adding the same token to a device group. It seems that FCM ignores the request to subscribe/add a Registration token if it's already subscribed/added to a device group.

这篇关于Firebase云消息传递-管理注册令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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