调用FirebaseInstanceId.getToken的行为 [英] Behavior of calling FirebaseInstanceId.getToken

查看:119
本文介绍了调用FirebaseInstanceId.getToken的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于FirebaseInstanceId.getToken(String authorizedEntity, String scope),我有两种类型的问题,一种是有关多次调用此方法的问题,另一种是有关是否调用此方法会触发FirebaseMessagingService.onNewToken(String token)的问题.

I have two types of questions around FirebaseInstanceId.getToken(String authorizedEntity, String scope), one around calling this method multiple times and one around whether calling this method triggers FirebaseMessagingService.onNewToken(String token).

1)多次拨打: 根据此文档为了能够接收来自多个发件人的消息,将多次调用getToken(String authorizedEntity, String scope),每次使用不同的发件人ID.我的问题是,每个呼叫都将返回不同的令牌,还是每个呼叫都将返回相同的令牌,但是现在令牌也可用于多个发件人?如果我们使用以前使用过的发件人ID调用此方法,它将返回现有令牌还是生成新令牌?

1) Calling multiple times: According to this documentation one would call getToken(String authorizedEntity, String scope) multiple times, each time with a different sender id, in order to be able to receive messages from multiple senders. My question is, will each call return a different token, or will each call return the same token but now the token will work also for multiple senders? If we call this method with a sender id that we've previously used before, will that return the existing token or generate a new one?

所以,说我有这个操作顺序

So, say I have this order of operation

  1. 致电getToken("senderId1", "FCM")并获取令牌A
  2. 致电getToken("senderId2", "FCM").我会得到A还是其他令牌B?
  3. 致电getToken("senderId2", "FCM").我会得到AB还是另外一个不同的C?
  1. Call getToken("senderId1", "FCM") and get token A
  2. Call getToken("senderId2", "FCM"). Will I get A or a different token B?
  3. Call getToken("senderId2", "FCM"). Will I get A, B, or yet another different one C?

2)是否会调用onNewToken? 此文档指出该方法将是如果令牌更改,则调用.那么这是否意味着如果getToken返回的令牌与之前不同,那么onNewToken也将被调用?如果我们要多次调用getToken以便允许来自不同发件人的接收,并且每次调用都返回不同的令牌,则onNewToken将继续被调用.

2) Will onNewToken be called? This documentation states that the method will be invoked if the token changes. So does this mean that if getToken returns a different token than before then onNewToken will be invoked as well? If we're going to be calling getToken multiple times to allow for receiving from different senders, and each call returns a different token, then onNewToken will keep getting invoked.

由于建议我们在触发onNewToken时更新服务器,因此我想了解预期的行为,并避免一般性地在每次onNewToken调用时更新服务器.

Since it is advised that we update our server when onNewToken is triggered, I want to understand the expected behavior and avoid generally updating the server on each invocation of onNewToken.

推荐答案

我的问题是,每个呼叫将返回不同的令牌,还是每个呼叫将返回相同的令牌,但是现在令牌也可用于多个发件人?

getToken()/getToken(String, String)将返回相同的令牌,直到相应的令牌到期为止.请注意,同样的标记,是指它们为每个发件人返回的相同的标记.即:

getToken() / getToken(String, String) will return the same token until such time that the corresponding token expires. Note that by same token, I mean the same token that they return for each sender. i.e.:

  • getToken()返回默认项目的注册令牌(例如tokenDefaultSenderId)
  • getToken(String, String)返回与之关联的发件人的注册令牌(例如tokenSenderId2)
  • getToken() returns the registration token for the default project (e.g. tokenDefaultSenderId)
  • getToken(String, String) returns the registration token for the sender it is associated to (e.g. tokenSenderId2)

如果我们使用以前使用过的发件人ID调用此方法,会返回现有令牌还是生成新令牌?

  1. 好的.
  2. 您将获得令牌B.
  3. 您将再次获得令牌B.
  1. Okay.
  2. You will get token B.
  3. You will get token B again.

令牌已绑定到与其关联的发件人.

A token is tied to the sender it is associated to.

会调用onNewToken吗? ...那么这是否意味着如果getToken返回的令牌与之前不同,那么onNewToken也将被调用?

onNewToken()将仅返回默认发送者的令牌(重点是我的):

onNewToken() will only return the token for the default sender (emphasis mine):

在为默认Firebase项目生成新令牌时调用.

Called when a new token for the default Firebase project is generated.

关于onNewToken()的事情是,它仅在前一个令牌已过期时才触发-要问的是,如果默认发送方的令牌已过期,其他发送方还有什么?因此,最好的解决方法是为您拥有的每个发件人调用getToken(),就像这样:

The thing about onNewToken() is that it triggers only when the previous token has expired -- the thing to ask is, if the token for the default sender expires, what more for the other senders? So the best workaround here is to call getToken() for each of the sender that you have, like so:

public void onNewToken(String token){
    String default = token;
    String sender2 = getToken("senderId2", "FCM");
    // and so on for each of your sender, then save the tokens as needed
}

这篇关于调用FirebaseInstanceId.getToken的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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