You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
function openWebSocket() {
|
|
console.log('open websocket');
|
|
}
|
|
|
|
self.addEventListener('install', function(event) {
|
|
self.skipWaiting();
|
|
console.log('Installed', event);
|
|
});
|
|
self.addEventListener('activate', (event) => {
|
|
// event.waitUntil(self.clients.claim());
|
|
event.waitUntil(openWebSocket());
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('push', (event) => {
|
|
const data = event.data.json();
|
|
|
|
const title = data.title || 'New Message';
|
|
const options = {
|
|
body: data.body || 'You have a new message',
|
|
icon: 'path/to/icon.png',
|
|
badge: 'path/to/badge.png',
|
|
vibrate: [200, 100, 200], // 振动
|
|
// Other notification options
|
|
};
|
|
|
|
event.waitUntil(
|
|
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
|