Push API Guide: How to Send Push Message to Application | Hacker Noon

June 12th 2020

Author profile picture

@mozillaMozilla Contributors

Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape.

The Push API gives web applications the ability to receive messages pushed to them from a server, whether or not the web app is in the foreground, or even currently loaded, on a user agent. This lets developers deliver asynchronous notifications and updates to users that opt in, resulting in better engagement with timely new content.

Push concepts and usage

When implementing PushManager subscriptions, it is vitally important that you protect against CSRF/XSRF issues in your app. See the following articles for more information:

This should be done in response to a user gesture, such as clicking a button, for example:

btn.addEventListener('click', function() {
  .then(function(pushSubscription) {
    // handle subscription

This is not only best practice — you should not be spamming users with notifications they didn’t agree to — but going forward browsers will explicitly disallow notifications not triggered in response to a user gesture. Firefox is already doing this from version 72, for example.

The resulting


includes all the information that the application needs to send a push message: an endpoint and the encryption key needed for sending data.

The service worker will be started as necessary to handle incoming push messages, which are delivered to the


event handler.

Each subscription is unique to a service worker.  The endpoint for the subscription is a unique capability URL: knowledge of the endpoint is all that is necessary to send a message to your application. The endpoint URL therefore needs to be kept secret, or other applications might be able to send push messages to your application.

Activating a service worker to deliver a push message can result in increased resource usage, particularly of the battery. Different browsers have different schemes for handling this, there is currently no standard mechanism. Both Firefox and Chrome allow any number of Push messages that generate notifications. Firefox, but not Chrome, allows a limited number (quota) of push messages to be sent to an application that don’t display a notification. The quota is refreshed each time the site is visited.

Note: As of Gecko 44, the allowed quota of push messages per application is not incremented when a new notification fires, when another is still visible, for a period of three seconds. This handles cases where a burst of Push messages is received, and not all generate a visible notification.

Note: Chrome versions earlier than 52 require you to set up a project on

Google Cloud Messaging to send push messages, and use the associated project number and API key when sending push notifications. It also requires an app manifest, with some special parameters to use this service.


Provides a way to receive notifications from third-party servers, as well as request URLs for push notifications. This interface has replaced the functionality offered by the obsolete



Provides access to push data sent by a server, and includes methods to manipulate the received data.

Provides a subcription’s URL endpoint, and allows unsubscription from a push service.

Service worker additions

The following additions to the Service Worker API have been specified in the Push API spec to provide an entry point for using Push messages. They also monitor and respond to push and subscription change events.
Returns a reference to the


interface for managing push subscriptions including subscribing, getting an active subscription, and accessing push permission status. This is the entry point into using Push messaging.

An event handler fired whenever a


event occurs; that is, whenever a server push message is received.

An event handler fired whenever a


event occurs; for example, when a push subscription has been invalidated, or is about to be invalidated (e.g. when a push service sets an expiration time.)



Browser compatibility



See also


Author profile picture

Read my stories

Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape.


The Noonification banner

Subscribe to get your daily round-up of top tech stories!

read original article here