// AR Cars — Firebase Messaging Service Worker // ===================================================== // This file MUST be named exactly: firebase-messaging-sw.js // and placed in the ROOT of your driver site on Hostinger // ===================================================== importScripts('https://www.gstatic.com/firebasejs/10.7.0/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/10.7.0/firebase-messaging-compat.js'); firebase.initializeApp({ apiKey: "AIzaSyDqzJCXbDOEjxC_HILz_7r62Lrz8dr0r08", authDomain: "arcars-ea01f.firebaseapp.com", projectId: "arcars-ea01f", storageBucket: "arcars-ea01f.firebasestorage.app", messagingSenderId: "657650126765", appId: "1:657650126765:web:6503a9f6300dbe4770a7dd" }); const messaging = firebase.messaging(); // Handle background messages (when app is closed or in background) messaging.onBackgroundMessage(function(payload) { console.log('Background message received:', payload); const title = payload.notification?.title || 'AR Cars'; const body = payload.notification?.body || 'You have a new update'; const icon = payload.notification?.icon || '/icons/icon-192.png'; const badge = '/icons/icon-96.png'; const data = payload.data || {}; const notificationOptions = { body: body, icon: icon, badge: badge, tag: data.tag || 'uc-notification', data: data, requireInteraction: true, actions: [ { action: 'open', title: 'Open App' }, { action: 'dismiss', title: 'Dismiss' } ] }; self.registration.showNotification(title, notificationOptions); }); // Handle notification click self.addEventListener('notificationclick', function(event) { event.notification.close(); if (event.action === 'dismiss') return; const data = event.notification.data || {}; let url = '/dashboard.html'; // Route to correct page based on notification type if (data.type === 'new_job') url = '/jobs.html'; else if (data.type === 'school_run_assigned') url = '/schoolruns.html'; else if (data.type === 'school_run_changed') url = '/schoolruns.html'; else if (data.type === 'job_updated') url = '/jobs.html'; event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clientList) { // If app is already open, focus it for (const client of clientList) { if (client.url.includes(self.location.origin) && 'focus' in client) { client.navigate(url); return client.focus(); } } // Otherwise open new window if (clients.openWindow) { return clients.openWindow(url); } }) ); }); // Cache essential files for offline use const CACHE_NAME = 'uc-driver-v1'; const OFFLINE_URLS = [ '/index.html', '/dashboard.html', '/schoolruns.html', '/jobs.html', '/earnings.html', '/vehiclecheck.html', '/office.html', '/history.html', '/manifest.json', '/icons/icon-192.png' ]; self.addEventListener('install', function(event) { event.waitUntil( caches.open(CACHE_NAME).then(function(cache) { return cache.addAll(OFFLINE_URLS); }) ); self.skipWaiting(); }); self.addEventListener('activate', function(event) { event.waitUntil( caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.filter(name => name !== CACHE_NAME).map(name => caches.delete(name)) ); }) ); self.clients.claim(); }); // Serve cached files when offline self.addEventListener('fetch', function(event) { if (event.request.method !== 'GET') return; if (!event.request.url.startsWith(self.location.origin)) return; event.respondWith( fetch(event.request).catch(function() { return caches.match(event.request).then(function(cached) { return cached || caches.match('/dashboard.html'); }); }) ); });