Create a Subscription Prompt widget that fully fits your website
Creating a subscription widget
If the default widget does not match your goals and preferences, you can create your custom subscription widget by following the steps described below.
Add to your project a hidden <div> containing the prompt to subscribe.
3. Once the Pushwoosh WebSDK is initialized, check whether a user is subscribed already or has blocked permission requests. If not, display the widget.
Make sure to display widget AFTER the WebSDK is fully initialized. Otherwise, the widget won't work correctly.
<script>
var $subscriptionWidget = document.getElementById('push-notification-widget');
var Pushwoosh = Pushwoosh || [];
// show a success message if a device subscribes to push notifications
Pushwoosh.push(['onSubscribe', () => {
$subscriptionWidget.classList.add('push-notification-widget_subscribed');
}]);
Pushwoosh.push(() => {
/* now WebSDK is ready and you can use public methods. */
// create an array for Promise.all
var actions = [];
// check device's permission on push notifications
var permission = Pushwoosh.driver.getPermission();
// check whether a device is registered in Pushwoosh and can receive push notifications
var isSubscribed = Pushwoosh.isSubscribed();
actions.push(permission);
actions.push(isSubscribed);
// wait for all actions resolved
Promise.all(actions)
.then((result) => {
var permission = result[0];
var isSubscribed = result[1];
// if push notifications are not blocked on a device (can show native permission prompt)
// and if the device is not subscribed to push notifications (including manual unsubscribe)
// show notification widget
if (permission !== 'denied' && !isSubscribed) {
$subscriptionWidget.classList.remove('push-notification-widget_hidden');
}
});
});
</script>