By default, users can manage their push preferences from their browser settings on desktop and mobile (see more here).
Instead of making your users dive into their browser settings, you can give them the option to disable/enable push notifications directly from your website using a toggle switch.
The following steps will help you set up this button on your website.
1. Getting the opt-in status of the user
First, you need to get the opt-in status of the user to update the status of the button. You can use this method :
batchSDK(function (api) {api.isSubscribed()});
2. Update the opt-in status
Depending on the action your users perform, you will need to call different methods when users click your toggle switch.
Turning off push notifications:
batchSDK(function(api) {api.unsubscribe();});
Turning on push notification:
batchSDK(function(api) {api.subscribe();});
Here is an example of how you could implement a toggle switch using these methods:
<input type="checkbox" id="toggle" onclick="validate();" />
<script type="text/javascript">
function validate() {
if (document.getElementById("toggle").checked) {
batchSDK(function(api) {api.subscribe();});
} else {
batchSDK(function(api) {api.unsubscribe();});
}
}
</script>
🚧 Important note: Keep in mind that notifications management may happen at two different levels:
at the SDK level
in the User's browser settings: on this level, you cannot control notifications with a toggle switch button as described above.
More details on Batch Public API :