PushEvent: data プロパティ
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2023年3月以降、すべてのブラウザーで利用可能です。
data は PushEvent インターフェイスの読み取り専用プロパティで、PushSubscription に送信されたデータを含む PushMessageData オブジェクトへの参照を返します。
値
PushMessageData オブジェクトです。
例
次の例は、 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 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",
);
});
});
仕様書
| 仕様書 |
|---|
| Push API> # dom-pushevent-data> |