Firebase云消息传递 - 处理注销 [英] Firebase Cloud Messaging - Handling logout
问题描述
我试过了
如何处理这种情况,当用户退出我的应用程序时,我不再希望他收到设备的通知。
$ b
FirebaseInstanceId.getInstance()。deleteToken(FirebaseInstanceId.getInstance()。getId(),FirebaseMessaging.INSTANCE_ID_SCOPE)
但我仍收到通知到我的设备 registration_id
。
我也确定这是我应该删除的令牌:
FirebaseInstanceId.getInstance ().getToken(FirebaseInstanceId.getInstance()。getId(),FirebaseMessaging.INSTANCE_ID_SCOPE)
FirebaseInstanceId.getInstance()。getToken()
)。
$ b
我也尝试了 FirebaseInstanceId。 getInstance()。deleteInstanceId()
,但接下来我调用 FirebaseInstanceId.getInstance.getToken
我收到null(它在第二次尝试)。
I g在 deleteInstanceId
之后,我可以立即再次调用 getToken()
,但是看起来像是一个黑客。另外还有这个答案,其中指出不应该这样做,但是建议删除显然不包含的标记工作。
那么正确的方法是什么?
好。所以我设法做了一些测试,并得出以下结论:
-
deleteToken()
是getToken(String,String)
的对象,但不是getToken()
。 b
只有当你传递的发件人ID是一个不同的发件人ID(不同于在google-services.json中可以看到的ID) 。例如,您希望允许不同的服务器发送到您的应用程序,您可以调用
getToken(THEIR_SENDER_ID,FCM)
em>发送到您的应用程序。这将返回一个不同的注册令牌,只对应于特定的发件人。
未来,如果您选择删除其授权发送到您的应用程序,您将不得不使用
deleteToken(THEIR_SENDER_ID,FCM)
。这将使相应的令牌失效,当发送者尝试发送消息时,如预期的行为,他们将收到一个NotRegistered
错误。- 为了删除您自己的发件人的令牌,正确的处理是使用
deleteInstanceId()
。
特别提到这个回答通过@Prince ,特别是帮助我这个代码示例。
$ b 由于@MichałK已经在他的职位,在调用,
getToken()
来发送一个新的令牌请求。但是,您不必第二次调用它。只要onTokenRefresh()
被执行,它就会自动触发为你提供新的令牌。
deleteInstanceId()
>getToken()
> checkonTokenRefresh()
$ b 注意:调用deleteInstanceId()
不仅会删除令牌你自己的应用程序它会删除所有主题订阅和所有其他与应用程序实例相关的标记。
code> deleteToken()正确吗?观众的价值应该是(也可以从我的答案中看出,你链接)是设置为应用服务器的发件人ID。您正在传递与发件人ID(包含应用程序实例ID值)不同的
getId()
值。此外,你如何发送消息(应用程序服务器或通知控制台)?
$ bgetToken()
和getToken(String,String)
返回不同的标记。请参阅我的答案此处。
$ b
我也尝试了
FirebaseInstanceId.getInstance()。deleteInstanceId()
,但接下来我再次调用FirebaseInstanceId.getInstance.getToken
我收到null(它在第二次尝试中可用)
这可能是因为您第一次调用
getToken()
时,它仍然在生成。这只是预期的行为。
我猜,在
deleteInstanceId
可以立即再次调用getToken()
,但是看起来像是一个黑客。
不是真的。这就是你如何得到新的生成(假设它已经生成)令牌。所以我觉得很好。
How do I handle situation, when user logs out of my application and I no longer want him to receive notifications to the device.
I tried
FirebaseInstanceId.getInstance().deleteToken(FirebaseInstanceId.getInstance().getId(), FirebaseMessaging.INSTANCE_ID_SCOPE)
But I still receive the notifications to my device's
registration_id
.I also made sure that this is the token I should delete:
FirebaseInstanceId.getInstance().getToken(FirebaseInstanceId.getInstance().getId(), FirebaseMessaging.INSTANCE_ID_SCOPE)
or simply
FirebaseInstanceId.getInstance().getToken()
).I also tried
FirebaseInstanceId.getInstance().deleteInstanceId()
, but then the next time I callFirebaseInstanceId.getInstance.getToken
I receive null (it works on the second try).I guess, after
deleteInstanceId
I could immediately callgetToken()
again, but it looks like a hack. And also there's this answer which states that it shouldn't be done, but it proposes deleting the token which apparently doesn't work.So what is the right method to handle this?
解决方案Okay. So I managed to do some testing and have concluded the following:
deleteToken()
is the counterpart ofgetToken(String, String)
, but not forgetToken()
.
It only works if the Sender ID you are passing is a different Sender ID (not the same ID that can be seen in your google-services.json). For example, you want to allow a different Server to send to your app, you call
getToken("THEIR_SENDER_ID", "FCM")
to give them authorization to send to your app. This will return a different registration token that corresponds only to that specific sender.In the future, if you chose to remove their authorization to send to your app, you'll then have to make use of
deleteToken("THEIR_SENDER_ID", "FCM")
. This will invalidate the corresponding token, and when the Sender attempts to send a message, as the intended behavior, they will receive aNotRegistered
error.- In order to delete the token for your own Sender, the correct handling is to use
deleteInstanceId()
.
Special mentioning this answer by @Prince, specifically the code sample for helping me with this.
As @MichałK already doing in his post, after calling the
deleteInstanceId()
,getToken()
should be called in order to send a request for a new token. However, you don't have to call it the second time. So long asonTokenRefresh()
is implemented, it should automatically trigger providing you the new token.For short,
deleteInstanceId()
>getToken()
> checkonTokenRefresh()
.Note: Calling
deleteInstanceId()
will not only delete the token for your own app. It will delete all topic subscriptions and all other tokens associated with the app instance.
Are you positive you're calling
deleteToken()
properly? The value for audience should be (also seen from my answer that you linked) is "set to the app server's sender ID". You're passing thegetId()
value which is not the same as the Sender ID (it contains the app instance id value). Also, how are you sending the message (App Server or Notifications Console)?getToken()
andgetToken(String, String)
returns different tokens. See my answer here.I also tried
FirebaseInstanceId.getInstance().deleteInstanceId()
, but then the next time I callFirebaseInstanceId.getInstance.getToken
I receive null (it works on the second try).It's probably because the first time you're calling the
getToken()
, it's still being generated. It's just the intended behavior.I guess, after
deleteInstanceId
I could immediately callgetToken()
again, but it looks like a hack.Not really. It's how you'll get the new generated (provided that it is already generated) token. So I think it's fine.
这篇关于Firebase云消息传递 - 处理注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 为了删除您自己的发件人的令牌,正确的处理是使用