Firebase 云消息传递 - 处理注销 [英] Firebase Cloud Messaging - Handling logout

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

问题描述

当用户退出我的应用程序并且我不再希望他收到设备通知时,我该如何处理.

How do I handle situation, when user logs out of my application and I no longer want him to receive notifications to the device.

我试过了

FirebaseInstanceId.getInstance().deleteToken(FirebaseInstanceId.getInstance().getId(), FirebaseMessaging.INSTANCE_ID_SCOPE)

但我仍然收到通知到我设备的registration_id.

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)

或简单地FirebaseInstanceId.getInstance().getToken()).

我也尝试过 FirebaseInstanceId.getInstance().deleteInstanceId(),但是下次我调用 FirebaseInstanceId.getInstance.getToken 时我收到 null(它适用于第二次尝试).

I also tried FirebaseInstanceId.getInstance().deleteInstanceId(), but then the next time I call FirebaseInstanceId.getInstance.getToken I receive null (it works on the second try).

我想,在 deleteInstanceId 之后我可以立即再次调用 getToken(),但它看起来像一个黑客.还有 this answer 指出不应该这样做,但它建议删除显然没有的令牌工作.

I guess, after deleteInstanceId I could immediately call getToken() 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:

  1. deleteToken()getToken(String, String) 的对应物,但不是 getToken() 的对应物.
  1. deleteToken() is the counterpart of getToken(String, String), but not for getToken().

仅当您传递的发件人 ID 是不同的发件人 ID(与您在 google-services.json 中可以看到的 ID 不同)时才有效.例如,您希望允许不同的服务器发送到您的应用程序,您可以调用 getToken("THEIR_SENDER_ID", "FCM") 授予他们授权发送到您的应用程序应用程序.这将返回一个仅对应于该特定发件人的不同注册令牌.

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.

将来,如果您选择删除他们的授权以发送到您的应用程序,则必须使用 deleteToken("THEIR_SENDER_ID", "FCM").这将使相应的令牌无效,并且当 Sender 尝试发送消息时,作为预期的行为,他们将收到 NotRegistered 错误.

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 a NotRegistered error.

  1. 为了删除您自己的 Sender 的令牌,正确的处理是使用 deleteInstanceId().

特别提到这个 @Prince 的回答,特别是帮助我解决这个问题的代码示例.

Special mentioning this answer by @Prince, specifically the code sample for helping me with this.

正如@MichałK 已经在他的帖子中所做的那样,在调用 deleteInstanceId() 之后,应该调用 getToken() 以发送对新令牌的请求.但是,您不必第二次调用它.只要实现了 onTokenRefresh() onNewToken(),它就会自动触发为您提供新令牌.

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 as onTokenRefresh() onNewToken() is implemented, it should automatically trigger providing you the new token.

简而言之,deleteInstanceId() > getToken() > 检查onTokenRefresh() onNewToken().

For short, deleteInstanceId() > getToken() > check onTokenRefresh() onNewToken().

注意:调用 deleteInstanceId() 不仅会删除您自己应用的令牌.它将删除与应用实例关联的所有主题订阅和所有其他令牌.

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.

您确定正确调用 deleteToken() 吗?受众的价值应该是(也可以从我链接的答案中看出)是设置为应用程序服务器的发件人 ID".您传递的 getId() 值与发件人 ID 不同(它包含应用实例 ID 值).另外,您如何发送消息(应用服务器或通知控制台)?

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 the getId() 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()getToken(String, String) 返回不同的标记.在此处查看我的回答.

getToken() and getToken(String, String) returns different tokens. See my answer here.

我也尝试过 FirebaseInstanceId.getInstance().deleteInstanceId(),但是下次我调用 FirebaseInstanceId.getInstance.getToken 时我收到 null(它第二次尝试有效).

I also tried FirebaseInstanceId.getInstance().deleteInstanceId(), but then the next time I call FirebaseInstanceId.getInstance.getToken I receive null (it works on the second try).

这可能是因为您第一次调用 getToken() 时,它仍在生成中.这只是预期的行为.

It's probably because the first time you're calling the getToken(), it's still being generated. It's just the intended behavior.

我想,在 deleteInstanceId 之后我可以立即再次调用 getToken(),但它看起来像一个黑客.

I guess, after deleteInstanceId I could immediately call getToken() 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屋!

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