tangled
alpha
login
or
join now
bad-example.com
/
spacedust-utils
6
fork
atom
demos for spacedust
6
fork
atom
overview
issues
pulls
pipelines
focus or open app on notification click
bad-example.com
8 months ago
2d74792c
e916a0ff
+29
-4
1 changed file
expand all
collapse all
unified
split
atproto-notifications
public
service-worker.js
+29
-4
atproto-notifications/public/service-worker.js
···
1
1
self.addEventListener('push', handlePush);
2
2
+
self.addEventListener('notificationclick', handleNotificationClick);
2
3
3
4
const getDB = ((upgrade, v) => {
4
5
let instance;
···
31
32
});
32
33
};
33
34
34
34
-
async function handlePush(event) {
35
35
-
const { subject, source, source_record } = event.data.json();
35
35
+
async function handlePush(ev) {
36
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
67
-
event.waitUntil(self.registration.showNotification(title, {
68
68
+
const notification = self.registration.showNotification(title, {
68
69
icon,
69
70
body: source_record,
70
70
-
}));
71
71
+
// actions: [
72
72
+
// {'action': 'bsky', title: 'Bluesky'},
73
73
+
// {'action': 'spacedust', title: 'All notifications'},
74
74
+
// ],
75
75
+
});
76
76
+
77
77
+
ev.waitUntil(notification);
78
78
+
}
79
79
+
80
80
+
function handleNotificationClick(ev) {
81
81
+
ev.waitUntil((async () => {
82
82
+
ev.notification.close();
83
83
+
84
84
+
const clientList = await clients.matchAll({
85
85
+
type: 'window',
86
86
+
includeUncontrolled: true,
87
87
+
});
88
88
+
89
89
+
// focus the first available existing window/tab
90
90
+
for (const client of clientList)
91
91
+
return await client.focus();
92
92
+
93
93
+
// otherwise open a new tab
94
94
+
await clients.openWindow('/');
95
95
+
})());
71
96
}