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

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

问题描述

我对FirebaseInstanceId.getToken(String authorizedEntity, String scope)有两种疑问,一种是多次调用此方法,另一种是调用此方法是否会触发FirebaseMessagingService.onNewToken(字符串令牌).

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.

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

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天全站免登陆