Pushpad
Articles › Push Subscriptions, Web Push Notifications

Web Push: how to check if a push endpoint is still valid

  • # push-api

How can you verify that a push subscription is still valid (and not invalid or expired)? How can you detect if a user has unsubscribed and the subscription is no longer valid?

Currently the Web Push standard doesn't offer any specific way to validate if a push subscription is valid.

So, if you are thinking of periodically checking if the subscriptions (endpoints) in your database are still valid, you can't do that directly.

There is however a strategy to check if the subscriptions are still valid: you need to send a push message (notification) to the subscription and see the HTTP status code returned by the browser push service.

If the push service returns a 2xx status code it means that the subscription is still valid, otherwise you should analyze the error returned and delete the subscription (if the error is permanent). In particular you should pay attention to the 410 Gone status code, which indicates that the subscription is expired (e.g. the user has revoked the permission for notifications).

If you use Pushpad everything is already managed properly and you don't need to worry about subscription management: every time that you send a notification, Pushpad analyzes the status of each subscription and, depending on the status code, retries the delivery or moves the subscription to trash (the subscriptions can be easily restored or they are deleted permanently after some time, when it's safe to delete them). Pushpad also provides reports about expired subscriptions, invalid subscriptions, etc.

Another strategy implemented by Pushpad to detect the expired subscriptions immediately is the use of the pushsubscriptionchange event (which, sometimes, also allows to replace the expired subscription with a new one). Basically Pushpad keeps your subscription database clean, by updating the subscriptions and deleting the invalid / expired subscriptions automatically.