PushEvent
注意:此功能僅在 Service Workers 中可用。
PushEvent 介面屬於 Push API,表示已接收到的推送訊息。此事件被髮送到 ServiceWorker 的 全域性作用域。它包含從應用伺服器傳送到 PushSubscription 的資訊。
建構函式
PushEvent()-
建立一個新的
PushEvent物件。
例項屬性
繼承自其父級 ExtendableEvent 的屬性。附加屬性
PushEvent.data只讀-
返回一個對
PushMessageData物件的引用,該物件包含傳送給PushSubscription的資料。
例項方法
繼承自其父級 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",
);
});
});
規範
| 規範 |
|---|
| 推送 API # pushevent-interface |
瀏覽器相容性
載入中…