Reputation: 43851
Firebase notifications are convenient because they require virtually zero code to implement in an Android app and can be sent from the web console.
However, what's the best way to implement an opt-out feature, so that users can choose to not receive push notifications?
Upvotes: 1
Views: 1670
Reputation: 1657
It seems like overwriting onMessageReceived(..)
works just fine.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (isNotificationsEnabled()) {
super.onMessageReceived(remoteMessage);
} else {
Log.w(TAG, "FCM Notification not shown. Notifications disabled");
}
}
Upvotes: -1
Reputation: 9225
If you must implement a client side opt out then the best way would be to use the REST API and not the Firebase console. The REST API allows you to send data messages, these messages do not auto generate notifications and always result in an onMessageReceived callback, at that point it is up to your app what you want to do with that message.
Note however that you can use topic messaging, let users subscribe to topics if they want notifications and only send messages to topics. Another option would be to use Firebase Analytics to define specific audiences then you can compose messages that target specific cross sections of audiences and users outside of those cross sections would not receive messages.
Upvotes: 3