11

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
Hashan Seneviratne
  • 1,157
  • 13
  • 24
  • 1
    Why not just call the [unsubscribeFromTopic()](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#subscribe_the_client_app_to_a_topic) function when logging out? – AL. Nov 14 '16 at 13:21

1 Answers1

11

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.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Yes that's true but. Topics are related to grouping. E.g. Sports. So purpose of using topics is to communicate to a group of users. That is not what i want. I want to manage individual users. If i want to communicate to a single user is possible via the token. But let's say user is logged out. But still communication should be impossible. – Hashan Seneviratne Nov 14 '16 at 13:33
  • @HashanSenevirante Do you mean totally unregistering the user? – AL. Nov 14 '16 at 13:38
  • I got mixed up with the terms. Correct term is device registration. This is exactly what i wanted to know. Thanks a lot AL. – Hashan Seneviratne Nov 14 '16 at 18:58
  • `deleteToken` doesn't work, at least for the FCM (the messages keep coming despite using this method). `deleteId` doesn't exist anymore. `unsubscribe` only works for topics and that may not be always a solution (it's not for me). Is there anything that I may be missing? Check out my question: http://stackoverflow.com/questions/43193215/firebase-cloud-messaging-handle-logout – Michał Klimczak Apr 03 '17 at 20:03
  • also from there : To make sure that messages go to the intended user: The app server can maintain a mapping between the current user and the registration token. The client app can then check to ensure that messages it receives match the logged in user. – Dan Alboteanu Feb 02 '19 at 12:23
  • 1
    Can someone confirm if unregistering an app instance is still not recommended after switching to FCM? – BabyishTank Oct 21 '20 at 21:56