ServiceWorkerGlobalScope: push event
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is only available inService Workers.
Thepush event is sent to a service worker's global scope (represented by theServiceWorkerGlobalScope interface) when the service worker has received a push message.
This event is not cancelable and does not bubble.
In this article
Syntax
Use the event name in methods likeaddEventListener(), or set an event handler property.
addEventListener("push", (event) => { })onpush = (event) => { }Event type
Event properties
Inherits properties from its parent,ExtendableEvent. Additional properties:
PushEvent.dataRead onlyReturns a reference to a
PushMessageDataobject containing data sent to thePushSubscription.
Example
This example sets up a handler forpush events that takesJSON data, parses it, and dispatches the message for handling based on information contained within the message.
self.addEventListener("push", (event) => { let message = event.data.json(); switch (message.type) { case "init": doInit(); break; case "shutdown": doShutdown(); break; }});Specifications
| Specification |
|---|
| Push API> # extensions-to-the-serviceworkerglobalscope-interface> |
| Push API> # dom-serviceworkerglobalscope-onpush> |