How can I disable firebase notification on android client app

寵の児 提交于 2021-01-01 04:57:10

问题


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


回答1:


Use this.

FirebaseMessaging.getInstance().unsubscribeFromTopic("X");



回答2:


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 ;)



来源:https://stackoverflow.com/questions/41892631/how-can-i-disable-firebase-notification-on-android-client-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!