Restricting GCM/FCM notifications through unregist

2019-01-15 00:54发布

Is there a way to restrict Firebase push notification registration? Not looking for topics. Let's say I have a login function. I want signed in users to be subscribed for push notifications. Then token can be sent to server and stored.

At the same time when users log out, user should also be unregistered.

So is there a mechanism available currently? I went through the Firebase API documentation but I could not find anything on that.

Just want to know whether I have missed anything.

1条回答
仙女界的扛把子
2楼-- · 2019-01-15 01:31

For the difference on when to use deleteToken() vs deleteInstanceId(), refer to this answer.


Unregistering an app instance (device/user) on logout is not advisable since even GCM. As per the GCM docs

Developers should never unregister the client app as a mechanism for logout or for switching between users, for the following reasons:

  • A registration token isn't associated with a particular logged in user. If the client app unregisters and then re-registers, the app can receive the same registration token or a different registration token.

  • Unregistration and re-registration may each take up to five minutes to propagate. During this time messages may be rejected due to the unregistered state, and messages may go to the wrong user.

If you still want to proceed, you can still refer to the same docs above.

Unregistration and Unsubscription

There are three methods a client app can use to stop receiving messages and/or topic subscriptions from the app server:

  • InstanceID deleteToken() with audience set to the app server's sender ID and scope set to GCM. This stops messages (including topic messages) from a specific app server

  • InstanceID deleteID(). This stops messages from all previous registrations and topic subscriptions for all app servers

  • GCMPubSub unsubscribe(). This stops topic messages from the specific topic of the app server

For FCM, it should be the FirebaseInstanceId.

查看更多
登录 后发表回答