PushEvent
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is only available in Service Workers.
Giao diện PushEvent của Push API đại diện cho một thông báo push đã được nhận. Sự kiện này được gửi đến phạm vi toàn cục của một ServiceWorker. Nó chứa thông tin được gửi từ máy chủ ứng dụng đến một PushSubscription.
Hàm khởi tạo
PushEvent()-
Tạo một đối tượng
PushEventmới.
Thuộc tính phiên bản
Kế thừa các thuộc tính từ lớp cha, ExtendableEvent. Các thuộc tính bổ sung:
PushEvent.dataRead only-
Trả về một tham chiếu đến một đối tượng
PushMessageDatachứa dữ liệu được gửi đếnPushSubscription.
Phương thức phiên bản
Kế thừa các phương thức từ lớp cha, ExtendableEvent.
Ví dụ
Ví dụ sau lấy dữ liệu từ một PushEvent và hiển thị nó trên tất cả các client của service worker.
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",
);
});
});
Thông số kỹ thuật
| Specification |
|---|
| Push API> # pushevent-interface> |