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 |
浏览器兼容性
加载中…