demos for spacedust

focus or open app on notification click

+29 -4
+29 -4
atproto-notifications/public/service-worker.js
··· 1 1 self.addEventListener('push', handlePush); 2 + self.addEventListener('notificationclick', handleNotificationClick); 2 3 3 4 const getDB = ((upgrade, v) => { 4 5 let instance; ··· 31 32 }); 32 33 }; 33 34 34 - async function handlePush(event) { 35 - const { subject, source, source_record } = event.data.json(); 35 + async function handlePush(ev) { 36 + const { subject, source, source_record } = ev.data.json(); 36 37 37 38 let icon; 38 39 if (source.startsWith('app.bsky')) icon = '/icons/app.bsky.png'; ··· 64 65 65 66 new BroadcastChannel('notif').postMessage('heyyy'); 66 67 67 - event.waitUntil(self.registration.showNotification(title, { 68 + const notification = self.registration.showNotification(title, { 68 69 icon, 69 70 body: source_record, 70 - })); 71 + // actions: [ 72 + // {'action': 'bsky', title: 'Bluesky'}, 73 + // {'action': 'spacedust', title: 'All notifications'}, 74 + // ], 75 + }); 76 + 77 + ev.waitUntil(notification); 78 + } 79 + 80 + function handleNotificationClick(ev) { 81 + ev.waitUntil((async () => { 82 + ev.notification.close(); 83 + 84 + const clientList = await clients.matchAll({ 85 + type: 'window', 86 + includeUncontrolled: true, 87 + }); 88 + 89 + // focus the first available existing window/tab 90 + for (const client of clientList) 91 + return await client.focus(); 92 + 93 + // otherwise open a new tab 94 + await clients.openWindow('/'); 95 + })()); 71 96 }