final notif changes

This commit is contained in:
2026-04-14 11:10:44 +05:30
parent a1ac8e7933
commit 19dcd73b29
11 changed files with 685 additions and 94 deletions

View File

@@ -1,40 +0,0 @@
// 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('/')
})
)
})

View File

@@ -1,3 +1,29 @@
// Firebase Messaging — handles background push notifications
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()
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',
})
})
// Cache management
const CACHE = 'gj-__BUILD_TIME__'
self.addEventListener('install', (e) => {
@@ -23,9 +49,8 @@ self.addEventListener('notificationclick', (e) => {
e.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
if (clients.length > 0) {
const client = clients[0]
client.focus()
client.navigate('/')
clients[0].focus()
clients[0].navigate('/')
} else {
self.clients.openWindow('/')
}
@@ -34,12 +59,7 @@ self.addEventListener('notificationclick', (e) => {
})
self.addEventListener('fetch', (e) => {
// Only cache GET requests for same-origin non-API resources
if (
e.request.method !== 'GET' ||
e.request.url.includes('/api/')
) return
if (e.request.method !== 'GET' || e.request.url.includes('/api/')) return
e.respondWith(
caches.match(e.request).then((cached) => cached || fetch(e.request))
)