owencm
owencm

Reputation: 8874

How to measure the number of people disabling push notifications from my progressive web app?

I'm using the new web push API with service workers in my progressive web app but am worried about users disabling the push notification permission after originally enabling it.

What is the best way to detect users disabling the notification permission (i.e. add instrumentation/analytics) so I can track this?

Upvotes: 2

Views: 384

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56044

I believe what you're looking is covered in the Permissions API, which includes a change event. That event is fired both for the initial user decision and again if the user later changes their mind.

At its most basic, you could do:

if ('permissions' in navigator) {
  navigator.permissions.query({name:'notifications'}).then(function(notificationPerm) {
    // notificationPerm.state is one of 'granted', 'denied', or 'prompt'.
    // At this point you can compare notificationPerm.state to a previously
    // cached value, and also listen for changes while the page is open via
    // the onchange handler.

    notificationPerm.onchange = function() {
      // Permissions have changed while the page is open.
      // Do something based on the current notificationPerm.state value.
    };
  });
}

There are a few good resources out there demonstrating how this could be used:

The Permissions API is supported in Chrome as of version 43, and Firefox as of the upcoming version 45 release.

Upvotes: 3

Related Questions