PushEvent

Baseline 2023
Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

PushEventプッシュ API のインターフェイスで、受け取ったプッシュメッセージを表します。このイベントは、ServiceWorkerグローバルスコープに送信されます。これは、アプリケーションサーバーから PushSubscription に送信された情報を含みます。

Event ExtendableEvent PushEvent

コンストラクター

PushEvent()

新しい PushEvent オブジェクトを生成します。

インスタンスプロパティ

親である ExtendableEvent からプロパティを継承しています。追加プロパティ:

PushEvent.data 読取専用

PushSubscription に送信されたデータを含む PushMessageData オブジェクトへの参照を返します。

インスタンスメソッド

親である ExtendableEvent からメソッドを継承しています。

次の例は、PushEvent からデータを取得して、すべてのサービスワーカークライアントで表示しています。

js
self.addEventListener("push", (event) => {
  if (!(self.Notification && self.Notification.permission === "granted")) {
    return;
  }

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

  const notification = new self.Notification(title, {
    body: message,
    tag: "simple-push-demo-notification",
    icon,
  });

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

仕様書

Specification
Push API
# pushevent-interface

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
PushEvent
PushEvent() constructor
data

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

関連項目