How Push Notifications Work
Understand the various steps involved in the working of push notifications.
The process of sending push notifications can be divided into the following procedural steps.
Requesting user permission
We must obtain the user’s consent before sending push notifications. Without the user’s permission, we cannot display notifications. The Notifications API allows us to request permission from the user via a prompt. This is a one-time task.
The user can decline permission from the browser settings even if they had previously accepted it. If the user enables notifications, we can then begin displaying notifications.
Although the browser may automatically prompt the user for notification permission, it’s preferable to request it manually. This allows us to handle the user’s response and gives us control over when to display the consent prompt (such as in response to a button click). Requesting permission this way can increase the likelihood that the user will consent.
Generating and storing subscriptions
Subscriptions contain information like an endpoint URL and the user’s public key. These are required for the browser and the web app to establish a push notification channel. This allows the app server to send messages to the user’s browser. Each browser vendor has its own push server, which refers to the browser-device combination (a browser on a given device).
The subscription object is created once the user grants permission for notifications. This subscription object is then sent to the app server and stored. It’s important to correctly store a user’s subscription information to target the push messages to the right endpoint.
Note: Unregistering a service worker will revoke all existing push notification subscriptions associated with that service worker.
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy