Skip to content

Manage User Consent for Web Push Notifications

By default, the Pushwoosh SDK shows the native subscription prompt as soon as it is initialized. However, you might want to ask for user consent at a more appropriate time, for example, after the user has had a chance to interact with your website.

To prevent the SDK from automatically showing the subscription prompt upon initialization, set the communicationEnabled parameter to false in the init call. This gives you control over when to ask for push notification permission.

<script type="text/javascript" src="//cdn.pushwoosh.com/webpush/v3/pushwoosh-web-notifications.js" async></script>
<script type="text/javascript">
var Pushwoosh = Pushwoosh || [];
Pushwoosh.push(['init', {
// other initialization parameters...
communicationEnabled: false, // Disable communication to prevent automatic subscription prompts
}]);
</script>

Once you have disabled automatic subscription, you can prompt the user to subscribe at any time. When the user agrees to receive push notifications (e.g., by clicking a “Subscribe” button on your custom UI), you can enable communication by calling the setCommunicationEnabled method.

Pushwoosh.setCommunicationEnabled(status?: boolean): Promise<void>

Calling Pushwoosh.setCommunicationEnabled(true) enables communication with Pushwoosh services. Once enabled, the SDK will proceed to show the native browser permission prompt.

Here is an example of how you might use this method:

// Assuming you have a subscribe button with id="subscribe-button"
const subscribeButton = document.getElementById('subscribe-button');
subscribeButton.addEventListener('click', () => {
Pushwoosh.setCommunicationEnabled(true)
.then(() => {
console.log('User is subscribed to push notifications.');
// You can hide the subscribe button now
subscribeButton.style.display = 'none';
})
.catch((error) => {
console.error('Error subscribing user:', error);
});
});