Home > Software engineering >  How to turn off FCM's automatic notifications with code, when the App is in the background?
How to turn off FCM's automatic notifications with code, when the App is in the background?

Time:02-08

My app has an option to turn on/off FCM notifications.

I can not show FCM notification when the App in foreground.

But how to turn off the notification function of FCM when the App in background?

Turn off FCM functionality with code in Android Studio.

CodePudding user response:

Firebase Cloud Messaging has two payload types in its messages:

  • Notification payloads, which is handled by the system itself if the app is not active, and can be handled by your code if the app is active.
  • Data payloads, which are always handled by your code. This code can then display a notification, but it can also do something else (or nothing at all) with the data.

If you send a Notification message and the app is not actively being used, that message is handled by the system and there's no way to prevent it from being displayed.


So you have two options:

  • Always send message with (only) a data payload, so that your own code can handle it - and display a notification if the user hasn't disabled that.

  • Don't send a message with a notification payload if the user has disabled receiving notifications.

  •  Tags:  
  • Related