且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase云消息传递 - 处理注销

更新时间:2023-02-26 21:27:48

好。所以我设法做了一些测试,并得出以下结论:


  1. deleteToken() getToken(String,String)的对象,但不是 getToken()。 b


    只有当你传递的发件人ID是一个不同的发件人ID(不同于在google-services.json中可以看到的ID) 。例如,您希望允许不同的服务器发送到您的应用程序,您可以调用 getToken(THEIR_SENDER_ID,FCM) em>发送到您的应用程序。这将返回一个不同的注册令牌,只对应于特定的发件人。



    未来,如果您选择删除其授权发送到您的应用程序,您将不得不使用 deleteToken(THEIR_SENDER_ID,FCM)。这将使相应的令牌失效,当发送者尝试发送消息时,如预期的行为,他们将收到一个 NotRegistered 错误。


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

    特别提到这个回答通过@Prince ,特别是帮助我这个代码示例。
    $ b 由于@MichałK已经在他的职位,在调用 getToken()来发送一个新的令牌请求。但是,您不必第二次调用它。只要 onTokenRefresh()被执行,它就会自动触发为你提供新的令牌。



    deleteInstanceId()> getToken()> check onTokenRefresh()


    $ b 注意:调用 deleteInstanceId()不仅会删除令牌你自己的应用程序它会删除所有主题订阅和所有其他与应用程序实例相关的标记。




    code> deleteToken()正确吗?观众的价值应该是(也可以从我的答案中看出,你链接)是设置为应用服务器的发件人ID。您正在传递与发件人ID(包含应用程序实例ID值)不同的 getId()值。此外,你如何发送消息(应用程序服务器或通知控制台)?
    $ b

    getToken() 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 call FirebaseInstanceId.getInstance.getToken I receive null (it works on the second try).

    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() is the counterpart of getToken(String, String), but not for getToken().

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

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

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

    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 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() and getToken(String, String) returns different tokens. See my answer here.

    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).

    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 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.