2

I want to disable firebase notification service when the user turns off notification through settings.

I found questions on this topic:

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

and

android FCM enable/disable from application settings

but answers are an alternative way for ignoring notifications.

Is there any other way to unregister firebase notifications.

Thankyou

Community
  • 1
  • 1
Mayur Kharche
  • 717
  • 1
  • 8
  • 27

3 Answers3

2

Use this.

FirebaseMessaging.getInstance().unsubscribeFromTopic("X");
josedlujan
  • 5,357
  • 2
  • 27
  • 49
2

I was facing the same issue, and i don't have a backend service that sends the push notifications through the FCM API.

What i tried and is working is to override the "handleIntent" method on my FirebaseMessagingService implementation and checking there if user disabled notifications or not from the app to handle them.

The working code below:

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun handleIntent(intent: Intent?) {
        // get from shared preferences if notifications are enabled

        val prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE) 
        val notificationsEnabled = prefs.getBoolean("notifications_enabled", false)

        if(notificationsEnabled){
            super.handleIntent(intent)
        }
    }

Hope it helps :)

  • Unfortunately only seems to work for me when the app is in the background, otherwise Firebase still pops the notification. – rjr-apps Jun 30 '23 at 17:26
  • not working to me, FCM auto generate notification when app in background/killed state – famfamfam Aug 21 '23 at 02:02
1

I spent a lot of time to find it out how to disable displaying push notifications when app in background. Everything is clear when app in foreground and you can handle everything in the app - when user switched off notifications in app settings, you just don't show notifications. But when app in background, Firebase handle everything somewhere under the hood and you cannot override methods of FirebaseMessagingService, because they are final. I found some very dirty solution and maby someone tell this is silly, but what I did is overrode onCreate of FirebaseMessagingService and did next:

    private void removePush(){
        new Handler().postDelayed(() -> {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= 23) {
                StatusBarNotification[] notifications = manager.getActiveNotifications();
                for (StatusBarNotification n : notifications) {
                    if (n.getTag().contains("campaign_collapse_key_")) {
                        Logger.logD(TAG + ": Found firebase push. remove it.");
                        manager.cancel(n.getTag(), n.getId());
                    }
                }
            }
            else{
                manager.cancelAll();
            }
        }, 500);
    }

If this solution help someone - good. If you found better solution, let everyone know ;)

Dmitriy Mitiai
  • 1,112
  • 12
  • 15