8e8aed69c0
Replace the browser Notification constructor (tab-must-be-open) with nostr-push Web Push notifications that work even when the browser is closed. - Add service worker (public/sw.js) for receiving and displaying push events - Add nostr-push RPC client (src/lib/nostrPush.ts) using an ephemeral device keypair for signing — the user's Nostr signer is never prompted - Add usePushNotifications hook managing the full lifecycle: SW registration, VAPID key fetch, pushManager.subscribe, and nostr-push registration - Replace the web path in useNativeNotifications with push registration that auto-enables/disables based on the notificationsEnabled encrypted setting - Android native Capacitor path is unchanged - Requires VITE_NOSTR_PUSH_PUBKEY env var (hex pubkey of the nostr-push server)
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
/**
|
|
* Ditto Service Worker
|
|
*
|
|
* Handles incoming Web Push notifications from the nostr-push server and
|
|
* opens/focuses the app when the user taps a notification.
|
|
*/
|
|
|
|
// --- Push received ---
|
|
|
|
self.addEventListener('push', (event) => {
|
|
if (!event.data) return;
|
|
|
|
let payload;
|
|
try {
|
|
payload = event.data.json();
|
|
} catch {
|
|
payload = { title: 'Ditto', body: event.data.text() };
|
|
}
|
|
|
|
const title = payload.title ?? 'Ditto';
|
|
const options = {
|
|
body: payload.body ?? '',
|
|
icon: payload.icon ?? '/icon-192.png',
|
|
badge: payload.badge ?? '/icon-192.png',
|
|
data: payload.data ?? {},
|
|
requireInteraction: false,
|
|
tag: payload.data?.subscription_id ?? 'ditto-notification',
|
|
renotify: true,
|
|
};
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(title, options),
|
|
);
|
|
});
|
|
|
|
// --- Notification click ---
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
event.waitUntil(
|
|
self.clients
|
|
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
.then((clientList) => {
|
|
// Focus an existing Ditto tab if one is open
|
|
for (const client of clientList) {
|
|
if (new URL(client.url).origin === self.location.origin) {
|
|
client.navigate('/notifications');
|
|
return client.focus();
|
|
}
|
|
}
|
|
// Otherwise open a new tab
|
|
return self.clients.openWindow('/notifications');
|
|
}),
|
|
);
|
|
});
|
|
|
|
// --- Activate immediately ---
|
|
|
|
self.addEventListener('install', () => self.skipWaiting());
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|