custom service-worker

dev/chat
Lei OT 1 year ago
parent e7c7d7b1be
commit 8d273a2e68

@ -7,7 +7,9 @@ self.addEventListener('install', function(event) {
console.log('Installed', event); console.log('Installed', event);
}); });
self.addEventListener('activate', (event) => { self.addEventListener('activate', (event) => {
// event.waitUntil(self.clients.claim());
event.waitUntil(openWebSocket()); event.waitUntil(openWebSocket());
self.clients.claim();
}); });
self.addEventListener('push', (event) => { self.addEventListener('push', (event) => {
@ -18,7 +20,7 @@ self.addEventListener('push', (event) => {
body: data.body || 'You have a new message', body: data.body || 'You have a new message',
icon: 'path/to/icon.png', icon: 'path/to/icon.png',
badge: 'path/to/badge.png', badge: 'path/to/badge.png',
vibrate: [200, 100, 200], vibrate: [200, 100, 200], // 振动
// Other notification options // Other notification options
}; };
@ -26,3 +28,12 @@ self.addEventListener('push', (event) => {
self.registration.showNotification(title, options) self.registration.showNotification(title, options)
); );
}); });
// Send a message to all clients every minute
setInterval(function() {
self.clients.matchAll().then(function(clients) {
clients.forEach(function(client) {
client.postMessage('Service worker is still running');
});
});
}, 60000); // 60000 ms = 1 minute

@ -100,4 +100,10 @@ if ('serviceWorker' in navigator) {
// } // }
// ); // );
}); });
// Listen for messages from the service worker
navigator.serviceWorker.addEventListener('message', function(event) {
console.log('Received message from service worker:', event.data);
});
} }

Loading…
Cancel
Save