41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
// Firebase Cloud Messaging service worker
|
|
// Config values are injected at build time by the Vite plugin (see vite.config.ts)
|
|
importScripts('https://www.gstatic.com/firebasejs/10.12.0/firebase-app-compat.js')
|
|
importScripts('https://www.gstatic.com/firebasejs/10.12.0/firebase-messaging-compat.js')
|
|
|
|
firebase.initializeApp({
|
|
apiKey: '__VITE_FIREBASE_API_KEY__',
|
|
authDomain: '__VITE_FIREBASE_AUTH_DOMAIN__',
|
|
projectId: '__VITE_FIREBASE_PROJECT_ID__',
|
|
messagingSenderId: '__VITE_FIREBASE_MESSAGING_SENDER_ID__',
|
|
appId: '__VITE_FIREBASE_APP_ID__',
|
|
})
|
|
|
|
const messaging = firebase.messaging()
|
|
|
|
// Handle background push messages (browser/PWA is closed or in background)
|
|
messaging.onBackgroundMessage((payload) => {
|
|
const title = payload.notification?.title || 'Grateful Journal 🌱'
|
|
const body = payload.notification?.body || "You haven't written today yet. Take a moment to reflect."
|
|
|
|
self.registration.showNotification(title, {
|
|
body,
|
|
icon: '/web-app-manifest-192x192.png',
|
|
badge: '/favicon-96x96.png',
|
|
tag: 'gj-daily-reminder',
|
|
})
|
|
})
|
|
|
|
self.addEventListener('notificationclick', (e) => {
|
|
e.notification.close()
|
|
e.waitUntil(
|
|
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
|
|
if (clients.length > 0) {
|
|
clients[0].focus()
|
|
return clients[0].navigate('/')
|
|
}
|
|
return self.clients.openWindow('/')
|
|
})
|
|
)
|
|
})
|