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