Skip to content Skip to sidebar Skip to footer

Push Notification Enable And Disable By Using Switch

I'm using firebase push notification(FCM).. and I want to enable and disable notifications using a switch button. For that I have shared preferences to enable and disable notificat

Solution 1:

FirebaseMessagingService runs in the background even when the app's not in the foreground so you won't be able to get the preferences using applicationContext. You should use Topic messaging - https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

Use this in your switch's change listener:

To enable push notifications -

FirebaseMessaging.getInstance().subscribeToTopic("your_topic");

To disable push notifications -

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

This way you will notify Firebase that you don't want to get notifications about a particular topic.

Post a Comment for "Push Notification Enable And Disable By Using Switch"