PushEvent: data 屬性
注意:此功能僅在 Service Workers 中可用。
PushEvent 介面的 data 只讀屬性返回對 PushMessageData 物件的引用,該物件包含傳送給 PushSubscription 的資料。
值
一個 PushMessageData 物件,如果在事件例項初始化時沒有傳入 data 成員,則為 null。
示例
以下示例從 PushEvent 中獲取資料並將其顯示給所有 service worker 的客戶端。
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",
);
});
});
規範
| 規範 |
|---|
| 推送 API # dom-pushevent-data |
瀏覽器相容性
載入中…