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