如何在Android客户端应用程序上禁用firebase通知

时间:2017-01-27 11:11:54

标签: android firebase notifications firebase-cloud-messaging

当用户通过设置关闭通知时,我想禁用firebase通知服务。

我发现了有关此主题的问题:

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

android FCM enable/disable from application settings

但是答案是忽略通知的另一种方式。

是否有其他方法可以取消注册firebase通知。

三江源

2 个答案:

答案 0 :(得分:0)

使用此功能。

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

答案 1 :(得分:0)

我花了很多时间来了解如何在后台运行应用程序时禁用显示推送通知。当应用程序处于前台状态时,所有内容都是清晰的,并且您可以处理应用程序中的所有内容-当用户在应用程序设置中关闭通知时,您只是不显示通知。但是,当应用程序在后台运行时,Firebase会在后台处理所有内容,并且您无法覆盖FirebaseMessagingService的方法,因为它们是最终的方法。 我发现了一个非常肮脏的解决方案,并且让老兄说这很愚蠢,但是我所做的却覆盖了FirebaseMessagingService的onCreate,然后执行了以下操作:

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

如果该解决方案可以帮助某人-很好。如果您找到更好的解决方案,请让所有人知道;)

相关问题