Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 822701 of PushMessageData

  • Revision slug: Web/API/PushMessageData
  • Revision title: PushMessageData
  • Revision id: 822701
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

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

The PushMessageData interface of the Push API provides access to push data sent by a server, and methods to manipulate the data.

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.

Example

self.addEventListener('push', function(event) {
  if (!(self.Notification && self.notification.permission === 'granted')) {
    return;
  }

  var data = {};
  if (event.data) {
    data = event.data.json();
  }
  var title = data.title || "Something Has Happened";
  var message = data.message || "Here's something you might want to check out.";
  var icon = "images/new-notification.png";

  var notification = new Notification(title, {
    body: message,
    tag: 'simple-push-demo-notification',
    icon: icon
  });

  notification.addEventListener('click', function() {
    if (clients.openWindow) {
      clients.openWindow('https://example.blog.com/2015/03/04/something-new.html');
    }
  };
});

Specifications

Specification Status Comment
{{SpecName('Push API', '#pushmessagedata-interface', 'PushManager')}} {{Spec2('Push API')}} Initial definition.

Browser Compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
{{CompatNo}}

See also

Revision Source

<p>{{APIRef("Push API")}}{{SeeCompatTable()}}</p>

<p>The <code><strong>PushMessageData</strong></code> interface of the <a href="/en-US/docs/Web/API/Push_API">Push API</a> provides access to push data sent by a server, and methods to manipulate the data.</p>

<h2 id="Properties">Properties</h2>

<p><em>None</em></p>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("PushMessageData.arrayBuffer()")}}</dt>
 <dd>Extracts the data as an {{domxref("ArrayBuffer")}} object.</dd>
 <dt>{{domxref("PushMessageData.blob()")}}</dt>
 <dd>Extracts the data as a {{domxref("Blob")}} object.</dd>
 <dt>{{domxref("PushMessageData.json()")}}</dt>
 <dd>Extracts the data as a <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON">JSON</a> object.</dd>
 <dt>{{domxref("PushMessageData.text()")}}</dt>
 <dd>Extracts the data as a plain text string.</dd>
</dl>

<h2 id="Example">Example</h2>

<pre class="brush: js">
self.addEventListener('push', function(event) {
&nbsp; if (!(self.Notification &amp;&amp; self.notification.permission === 'granted')) {
&nbsp;   return;
&nbsp; }

&nbsp; var data = {};
&nbsp; if (event.data) {
&nbsp;   data = event.data.json();
&nbsp; }
&nbsp; var title = data.title || "Something Has Happened";
&nbsp; var message = data.message || "Here's something you might want to check out.";
&nbsp; var icon = "images/new-notification.png";

&nbsp; var notification = new Notification(title, {
&nbsp;   body: message,
&nbsp;   tag: 'simple-push-demo-notification',
&nbsp;   icon: icon
&nbsp; });

&nbsp; notification.addEventListener('click', function() {
&nbsp;   if (clients.openWindow) {
&nbsp;     clients.openWindow('https://example.blog.com/2015/03/04/something-new.html');
&nbsp;   }
&nbsp; };
});</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Push API', '#pushmessagedata-interface', 'PushManager')}}</td>
   <td>{{Spec2('Push API')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Compatibility">Browser Compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th style="line-height: 16px;">Feature</th>
   <th style="line-height: 16px;">Android</th>
   <th style="line-height: 16px;">Android Webview</th>
   <th style="line-height: 16px;">Firefox Mobile (Gecko)</th>
   <th style="line-height: 16px;">Firefox OS</th>
   <th style="line-height: 16px;">IE Mobile</th>
   <th style="line-height: 16px;">Opera Mobile</th>
   <th style="line-height: 16px;">Safari Mobile</th>
   <th style="line-height: 16px;">Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>
    <table class="compat-table">
     <tbody>
      <tr>
       <td>{{CompatNo}}</td>
      </tr>
     </tbody>
    </table>
   </td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Push_API/Using_the_Push_API">Using the Push API</a></li>
 <li><a href="https://updates.html5rocks.com/2015/03/push-notificatons-on-the-open-web">Push Notifications on the Open Web</a>, Matt Gaunt</li>
</ul>
Revert to this revision