How to Check Website Visitor’s Push Subscription Status

When building a robust and effective communication strategy using web push notifications, it’s crucial to know the subscription status of your website visitors. This is especially important if you connect your existing database with your push subscriber list.

Understanding whether your logged-in users are subscribed to push notifications allows you to tailor your messaging, create more personalized experiences, and maximize the engagement rate of your notifications. This documentation will guide you through the process of using our API to check the subscription status of your website visitors.

Check Subscription Status Using Web SDK

Before checking the subscription status, ensure that the PushEngage installation is completed. If you do not have this integrated, follow this guide and complete your installation.

Here is the getPermission() method is a straightforward way to determine the subscription status of a user. This method returns the current push notification permission status of the user visiting your website.

The syntax for this Method is :

getPermission().then(successFunctionCallback).catch(errorFunctionCallback);

You can use it in the following way

PushEngage.push(function () {
  PushEngage.getPermission()
    .then(function (notificationPermission) {
      console.log(notificationPermission);
    })
    .catch(function (error) {
      console.log(error.message, error.details);
    });
});

If you are dealing with multiple cases based on responses or the user permission status. You can use the below sample code:

// Check the user's push notification subscription status
PushEngage.getPermission().then(function(permissionStatus) {
    console.log("User's push subscription status: ", permissionStatus);
    
    // Handle different subscription statuses
    switch(permissionStatus) {
        case 'default':
            console.log("User has not yet made a decision about push notifications.");
            break;
        case 'granted':
            console.log("User has granted permission for push notifications.");
            break;
        case 'denied':
            console.log("User has denied permission for push notifications.");
            break;
        default:
            console.log("Unknown permission status.");
    }
}).catch(function(error) {
    console.error("An error occurred while checking the subscription status: ", error);
});

The getPermission() method returns one of three possible values:

  • default: The user has not yet made a decision regarding push notifications.
  • granted: The user has been granted permission to receive push notifications.
  • denied: The user has been denied permission to receive push notifications.

Based on these values, you can decide how to engage with your users effectively. For instance, if the status is default, you may want to prompt the user to opt in to push notifications. If the status is denied, you may choose to offer an alternative engagement method.

If in case you run into any issues, please feel free to contact us by clicking here. Our support team would be able to help you.

Still stuck? How can we help?
Last updated on September 2nd, 2024

Engage and Retain Visitors AfterThey’ve Left Your Website

Increase the value of every web visit with Push Notifications that are hard to miss.

  • Forever Free Plan
  • Easy Setup
  • 5 Star Support