问题
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