mirror of
https://github.com/Tony0410/quietthanks.git
synced 2026-05-24 21:31:41 +08:00
Add PWA infrastructure for iOS notification support
- Add manifest.json for proper PWA installation - Add service worker (sw.js) for push notification handling - Add ServiceWorkerProvider to register SW and manage notifications - Generate PNG icons (192x192, 512x512) for iOS compatibility - Update settings to show notification status and test button - Use service worker showNotification for iOS Safari compatibility Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
94
public/sw.js
Normal file
94
public/sw.js
Normal file
@@ -0,0 +1,94 @@
|
||||
// Service Worker for Quiet Thanks PWA
|
||||
const CACHE_NAME = 'quiet-thanks-v1';
|
||||
|
||||
// Install event - cache essential assets
|
||||
self.addEventListener('install', (event) => {
|
||||
console.log('[SW] Installing service worker...');
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
// Activate event - clean up old caches
|
||||
self.addEventListener('activate', (event) => {
|
||||
console.log('[SW] Activating service worker...');
|
||||
event.waitUntil(clients.claim());
|
||||
});
|
||||
|
||||
// Push event - handle incoming push notifications
|
||||
self.addEventListener('push', (event) => {
|
||||
console.log('[SW] Push event received');
|
||||
|
||||
let data = {
|
||||
title: 'Quiet Thanks',
|
||||
body: 'Take a moment to reflect on what you\'re grateful for today.',
|
||||
icon: '/icons/icon.svg',
|
||||
badge: '/icons/icon.svg',
|
||||
tag: 'daily-reminder',
|
||||
};
|
||||
|
||||
if (event.data) {
|
||||
try {
|
||||
const payload = event.data.json();
|
||||
data = { ...data, ...payload };
|
||||
} catch (e) {
|
||||
data.body = event.data.text();
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
body: data.body,
|
||||
icon: data.icon,
|
||||
badge: data.badge,
|
||||
tag: data.tag,
|
||||
vibrate: [100, 50, 100],
|
||||
data: {
|
||||
url: '/',
|
||||
},
|
||||
actions: [
|
||||
{ action: 'open', title: 'Open app' },
|
||||
{ action: 'dismiss', title: 'Dismiss' },
|
||||
],
|
||||
};
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(data.title, options)
|
||||
);
|
||||
});
|
||||
|
||||
// Notification click handler
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
console.log('[SW] Notification clicked');
|
||||
event.notification.close();
|
||||
|
||||
if (event.action === 'dismiss') {
|
||||
return;
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
clients.matchAll({ type: 'window', includeUncontrolled: true })
|
||||
.then((clientList) => {
|
||||
// Focus existing window if available
|
||||
for (const client of clientList) {
|
||||
if (client.url.includes(self.registration.scope) && 'focus' in client) {
|
||||
return client.focus();
|
||||
}
|
||||
}
|
||||
// Open new window
|
||||
if (clients.openWindow) {
|
||||
return clients.openWindow('/');
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Periodic sync for background reminders (when supported)
|
||||
self.addEventListener('periodicsync', (event) => {
|
||||
if (event.tag === 'daily-reminder') {
|
||||
event.waitUntil(checkAndShowReminder());
|
||||
}
|
||||
});
|
||||
|
||||
async function checkAndShowReminder() {
|
||||
// This would check the reminder time and show notification if appropriate
|
||||
// For now, we rely on the client-side scheduler
|
||||
console.log('[SW] Periodic sync triggered');
|
||||
}
|
||||
Reference in New Issue
Block a user