Firebase on Android - I want to disable firebase notifications on Android client

martinodecarlo picture martinodecarlo · May 29, 2016 · Viewed 23.8k times · Source

I'm using Firebase console to send notifications to users. However I want to let users disable notifications. How can I disable them?

I am able to handle (and stop) notifications through FirebaseMessagingService only when the app is in foreground. But I cannot stop notifications when the app is in background.

Answer

Alvin Rusli picture Alvin Rusli · Sep 19, 2016

I couldn't comment yet, but racs' comment helped me a lot.

Actually, Firebase docs recommend to use data push instead of notifications if you want to have complete control over how it is handled: firebase.google.com/docs/cloud-messaging/… - quote: "Use notifications when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app..."

So basically, I changed the body for the push notification request from:

{
   "data": {
       "type" : "mytype"
    },
    "notification": {
        "title": "My Title",
        "body": "My Notification Message"
    },
    "to": "/topics/all"
}

To:

{
   "data": {
       "type" : "mytype",
       "title": "My Title",
       "body": "My Notification Message"
    },
    "to": "/topics/all"
}

Now my app calls onMessageReceived() everytime even in background, and I just changed the methods to use the obtained notification title and message in the push data.