1

So, I just want to handle the situation when user logs out from the app. I call method FirebaseMessaging.getInstance().deleteToken() when user logs out, and it works as expected, i.e. user doesn't receive push messages anymore...

But if there is no internet connection method deleteToken() doesn't help. User still gets push messages when a connection is established despite the fact that he logged out.

How can I handle this?

  • What I do on my apps is use a SharedPreference to keep track of whether the user is signed in or not. And, in my Firebase Cloud Messaging Service, before sending notification I check my SharedPreference to see if the user is signed in or not. They will get a notification payload, but it won't do anything. – Basu Jul 28 '21 at 19:13

1 Answers1

2

Firebase Cloud Messaging has no knowledge of the user, which explains why it can't change whether it sends/displays a message based on authentication status.

The main problem here is that notification messages are displayed by the system if the app is not being used, and you can't control what the system does with them based on authentication state.

The best way to deal with these situations is to only send data messages (without a notification property). The display of such data-only messages is handled by your application code, so your application code can check whether a user is signed-in, and decide whether to display the message based on that.

I also recommend reading up on FCM messages types to learn more about the distinction between these types.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Indeed! I just thought that writing logic to handle notifications on Android is bad practice. But now I have changed my mind with your help. Thx. – Artyom Danilin Jul 28 '21 at 18:32