Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
54 lines (39 loc) · 1.72 KB
/
Copy pathindex.md
File metadata and controls
54 lines (39 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
title: PushMessageData
slug: Web/API/PushMessageData
page-type: web-api-interface
browser-compat: api.PushMessageData
---
{{APIRef("Push API")}}{{SecureContext_Header}}{{AvailableInWorkers("service")}}
The **`PushMessageData`** interface of the [Push API](/en-US/docs/Web/API/Push_API) provides methods which let you retrieve the push data sent by a server in various formats.
Unlike the similar methods in the [Fetch API](/en-US/docs/Web/API/Fetch_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 the `PushMessageData` interface.
## Instance properties
None.
## Instance methods
- {{domxref("PushMessageData.arrayBuffer()")}}
- : Extracts the data as an {{jsxref("ArrayBuffer")}} object.
- {{domxref("PushMessageData.blob()")}}
- : Extracts the data as a {{domxref("Blob")}} object.
- {{domxref("PushMessageData.bytes()")}}
- : Extracts the data as a {{jsxref("Uint8Array")}} object.
- {{domxref("PushMessageData.json()")}}
- : Extracts the data as a [JSON](/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) object.
- {{domxref("PushMessageData.text()")}}
- : Extracts the data as a plain text string.
## Examples
```js
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
{{Specifications}}
## Browser compatibility
{{Compat}}