优化路径,添加消息推送
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 592 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 366 KiB |
@@ -0,0 +1,66 @@
|
||||
self.addEventListener("push", (event) => {
|
||||
let data = {
|
||||
title: "家人连线台",
|
||||
body: "收到一条新的家人留言。",
|
||||
url: "/",
|
||||
icon: "/pwa/icon-192x192.png",
|
||||
badge: "/pwa/badge-96x96.png",
|
||||
tag: "digital-human-message",
|
||||
};
|
||||
|
||||
if (event.data) {
|
||||
try {
|
||||
data = { ...data, ...event.data.json() };
|
||||
} catch {
|
||||
data = { ...data, body: event.data.text() };
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(data.title, {
|
||||
body: data.body,
|
||||
icon: data.icon,
|
||||
badge: data.badge,
|
||||
tag: data.tag,
|
||||
data: {
|
||||
url: data.url,
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
const targetUrl = new URL(
|
||||
event.notification.data?.url || "/",
|
||||
self.location.origin,
|
||||
).toString();
|
||||
|
||||
event.notification.close();
|
||||
|
||||
event.waitUntil(
|
||||
clients.matchAll({ type: "window", includeUncontrolled: true }).then(
|
||||
async (clientList) => {
|
||||
for (const client of clientList) {
|
||||
if (!("focus" in client)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (client.url === targetUrl) {
|
||||
return client.focus();
|
||||
}
|
||||
|
||||
if (client.url.startsWith(self.location.origin) && "navigate" in client) {
|
||||
await client.navigate(targetUrl);
|
||||
return client.focus();
|
||||
}
|
||||
}
|
||||
|
||||
if (clients.openWindow) {
|
||||
return clients.openWindow(targetUrl);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user