PushMessageData
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.
* Some parts of this feature may have varying levels of support.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is only available inService Workers.
ThePushMessageData interface of thePush API provides methods which let you retrieve the push data sent by a server in various formats.
Unlike the similar methods in theFetch API, which only allow the method to be invoked once, these methods can be called multiple times.
Messages received through the Push API are sent encrypted by push services and then automatically decrypted by browsers before they are made accessible through the methods of thePushMessageData interface.
In this article
Instance properties
None.
Instance methods
PushMessageData.arrayBuffer()Extracts the data as an
ArrayBufferobject.PushMessageData.blob()Extracts the data as a
Blobobject.PushMessageData.bytes()Extracts the data as a
Uint8Arrayobject.PushMessageData.json()Extracts the data as aJSON object.
PushMessageData.text()Extracts the data as a plain text string.
Examples
self.addEventListener("push", (event) => { const obj = event.data.json(); if (obj.action === "subscribe" || obj.action === "unsubscribe") { fireNotification(obj, event); port.postMessage(obj); } else if (obj.action === "init" || obj.action === "chatMsg") { port.postMessage(obj); }});Specifications
| Specification |
|---|
| Push API> # pushmessagedata-interface> |