From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/pushmessagedata/index.html | 145 +++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 files/zh-cn/web/api/pushmessagedata/index.html (limited to 'files/zh-cn/web/api/pushmessagedata/index.html') diff --git a/files/zh-cn/web/api/pushmessagedata/index.html b/files/zh-cn/web/api/pushmessagedata/index.html new file mode 100644 index 0000000000..bf38ec955b --- /dev/null +++ b/files/zh-cn/web/api/pushmessagedata/index.html @@ -0,0 +1,145 @@ +--- +title: PushMessageData +slug: Web/API/PushMessageData +tags: + - API + - Experimental + - Interface + - NeedsTranslation + - Push + - Push API + - PushMessageData + - Reference + - Service Workers + - TopicStub +translation_of: Web/API/PushMessageData +--- +

{{APIRef("Push API")}}{{SeeCompatTable()}}

+ +

The PushMessageData interface of the 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, which only allow the method to be invoked once, these methods can be called multiple times.

+ +

Properties

+ +

None.

+ +

Methods

+ +
+
{{domxref("PushMessageData.arrayBuffer()")}}
+
Extracts the data as an {{domxref("ArrayBuffer")}} object.
+
{{domxref("PushMessageData.blob()")}}
+
Extracts the data as a {{domxref("Blob")}} object.
+
{{domxref("PushMessageData.json()")}}
+
Extracts the data as a JSON object.
+
{{domxref("PushMessageData.text()")}}
+
Extracts the data as a plain text string.
+
+ +

Examples

+ +

In our Push API Demo, we send JSON objects via push messages from our server by first converting them to strings via {{jsxref("JSON.stringify()")}} (see server.js example):

+ +
webPush.sendNotification(subscriber[2], 200, obj.key, JSON.stringify({
+  action: 'chatMsg',
+  name: obj.name,
+  msg: obj.msg
+}));
+ +

When the message reaches the service worker, we convert the data back to a JSON object using {{domxref("PushMessageData.json()")}} before working out what to do with it next:

+ +
self.addEventListener('push', function(event) {
+  var 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

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Push API', '#pushmessagedata-interface', 'PushMessageData')}}{{Spec2('Push API')}}Initial definition.
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(50.0)}}{{CompatGeckoDesktop(44.0)}}[1]{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile(48)}}[2]{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(50.0)}}
+
+ + + +

See also

+ + -- cgit v1.2.3-54-g00ecf