I have implemented firebase push notification and it is working fine. But now I have one requirement that on the toggle button in application I need to on/off the firebase notifications. Can anyone suggest anything?
Asked
Active
Viewed 1,430 times
0

Frank van Puffelen
- 565,676
- 79
- 828
- 807

Sonal Bharwani
- 61
- 11
-
You can easily implement that by using `SharedPreferences` storing Boolean `true` or `false` on toggle button. And with retrieving the value from `SharedPreferences` according is toggle button on or off subscribe or unsubscribe to Firebase notifications. – Yupi Dec 11 '17 at 12:23
-
@Yupi You are talking about subscribe and unsubscribe topic here, right? – Sonal Bharwani Dec 11 '17 at 12:25
-
Yes I meant on that. – Yupi Dec 11 '17 at 12:27
-
I posted an answer if that is what you are looking for. – Yupi Dec 11 '17 at 12:50
-
Similarly to [handling logouts](https://stackoverflow.com/a/43197589/4625829), but instead, you disable the push notifs on a toggle. If you're using `data` message payloads only, then you should be fine with just disabling the Notification builder only. – AL. Dec 13 '17 at 01:25
1 Answers
0
You could implement that by using SharedPreferences
storing Boolean true
or false
on toggle button. And with retrieving the value from SharedPreferences
according is toggle button on or off subscribe or unsubscribe to Firebase notifications. For example:
For toggle switch:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("key", true).apply();
} else {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("key", false).apply();
}
}
});
And to get stored values:
boolean isSubscribed = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("key", false);
if(isSubscribed)
FirebaseMessaging.getInstance().subscribeToTopic('news');
else
FirebaseMessaging.getInstance().unsubscribeFromTopic('news');

Yupi
- 4,402
- 3
- 18
- 37