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