Pushpad
Articles › Push Subscriptions, Web Push Notifications

How to unsubscribe a user from web push notifications

  • # permission
  • # push-api

There are different strategies that you can use to allow a user to opt-out from the push notifications of a website that they have previously enabled.

Remove the push subscription from your database

When the user has enabled the notifications for your website, you have probably stored the push subscription in a database (it can be your own database or the database provided by a service like Pushpad).

When the user no longer wants to receive the notifications, you can allow them to unsubscribe using a button for example.

The button must trigger a function that gets the current endpoint and sends a request to the backend to remove it from the database.

This approach is similar to a mailing list, where you simply remove an address (an endpoint in this case) from the list when a user no longer wants to receive the messages.

If you are using Pushpad, there is a JavaScript function that does exactly that: you can simply call pushpad('unsubscribe').

Remove the permission for notifications

An alternative method is to revoke the browser permission:

In this case the push subscription will become invalid (expired) and any attempt to send a push message to it will fail (with an HTTP 410 Gone or 404 Not Found).

The server that sends the push messages should detect the error code and remove the expired subscription from the database. If you are using Pushpad we take care of that automatically.