增加浏览器离线通知功能
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/* MoonTVPlus Web Push handlers */
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(self.skipWaiting());
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener('push', (event) => {
|
||||
if (!event.data) return;
|
||||
|
||||
let payload = {};
|
||||
try {
|
||||
payload = event.data.json();
|
||||
} catch (error) {
|
||||
payload = { title: 'MoonTVPlus', body: event.data.text() };
|
||||
}
|
||||
|
||||
const title = payload.title || 'MoonTVPlus';
|
||||
const options = {
|
||||
body: payload.body || payload.message || '',
|
||||
icon: '/icons/icon-192x192.png',
|
||||
badge: '/icons/icon-192x192.png',
|
||||
tag: payload.notificationId || undefined,
|
||||
data: {
|
||||
url: payload.url || '/',
|
||||
notificationId: payload.notificationId,
|
||||
},
|
||||
};
|
||||
|
||||
event.waitUntil(self.registration.showNotification(title, options));
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
event.notification.close();
|
||||
|
||||
const targetUrl = new URL(event.notification.data?.url || '/', self.location.origin).href;
|
||||
|
||||
event.waitUntil((async () => {
|
||||
const windowClients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
|
||||
for (const client of windowClients) {
|
||||
if ('focus' in client) {
|
||||
await client.focus();
|
||||
if ('navigate' in client) {
|
||||
return client.navigate(targetUrl);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (self.clients.openWindow) {
|
||||
return self.clients.openWindow(targetUrl);
|
||||
}
|
||||
})());
|
||||
});
|
||||
Reference in New Issue
Block a user