fix notification test
This commit is contained in:
parent
a188f08726
commit
3e77f25b87
@ -41,8 +41,8 @@ function urlBase64ToUint8Array(base64String) {
|
||||
// This function handles the actual subscription process
|
||||
async function subscribeToNotifications() {
|
||||
try {
|
||||
const publicVapidKey = process.env.PUBLIC_VAPID_KEY; // Make sure this is set in your .env
|
||||
const token = localStorage.getItem('authToken');
|
||||
// CORRECT
|
||||
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU'; const token = localStorage.getItem('authToken');
|
||||
|
||||
if (!token || !publicVapidKey) {
|
||||
return console.error('Auth token or VAPID key is missing.');
|
||||
|
||||
81
public/sw.js
81
public/sw.js
@ -1,73 +1,54 @@
|
||||
// --- Time Pulse Service Worker ---
|
||||
// public/sw.js - CORRECTED
|
||||
|
||||
const CACHE_NAME = 'timepulse-v1';
|
||||
|
||||
// Files to cache (App Shell)
|
||||
const filesToCache = [
|
||||
'/',
|
||||
'/index.html',
|
||||
'/style/style.css',
|
||||
'/js/main.js',
|
||||
'/js/ui.js',
|
||||
'/js/api.js',
|
||||
'/js/utils.js',
|
||||
'/manifest.json',
|
||||
'/icons/icon-192x192.webp',
|
||||
'/icons/icon-512x512.webp'
|
||||
'/', '/index.html', '/style/style.css', '/js/main.js', '/js/ui.js',
|
||||
'/js/api.js', '/js/utils.js', '/manifest.json',
|
||||
'/icons/icon-192x192.webp', '/icons/icon-512x512.webp'
|
||||
];
|
||||
|
||||
// --- Install Event ---
|
||||
self.addEventListener('install', event => {
|
||||
console.log('[SW] Install event');
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(cache => {
|
||||
console.log('[SW] Caching app shell');
|
||||
return cache.addAll(filesToCache);
|
||||
})
|
||||
);
|
||||
self.skipWaiting(); // Activate worker immediately
|
||||
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(filesToCache)));
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
// --- Activate Event ---
|
||||
self.addEventListener('activate', event => {
|
||||
console.log('[SW] Activate event');
|
||||
event.waitUntil(
|
||||
caches.keys().then(keys =>
|
||||
Promise.all(
|
||||
keys.map(key => {
|
||||
if (key !== CACHE_NAME) {
|
||||
console.log('[SW] Removing old cache:', key);
|
||||
return caches.delete(key);
|
||||
}
|
||||
})
|
||||
)
|
||||
)
|
||||
caches.keys().then(keys => Promise.all(
|
||||
keys.map(key => {
|
||||
if (key !== CACHE_NAME) {
|
||||
return caches.delete(key);
|
||||
}
|
||||
})
|
||||
))
|
||||
);
|
||||
self.clients.claim(); // Become available to all pages
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
// --- Fetch Event ---
|
||||
self.addEventListener('fetch', event => {
|
||||
// Ignore non-GET requests and API calls
|
||||
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(response => {
|
||||
// Return cached file OR fetch from network
|
||||
return response || fetch(event.request).then(fetchResponse => {
|
||||
// Optionally cache new files on the fly
|
||||
return caches.open(CACHE_NAME).then(cache => {
|
||||
cache.put(event.request, fetchResponse.clone());
|
||||
return fetchResponse;
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
// Optional: return a fallback page or image here
|
||||
})
|
||||
caches.match(event.request).then(response => response || fetch(event.request))
|
||||
);
|
||||
});
|
||||
|
||||
// --- Push Event (The missing part!) ---
|
||||
self.addEventListener('push', e => {
|
||||
try {
|
||||
const data = e.data.json();
|
||||
self.registration.showNotification(data.title, {
|
||||
body: data.body,
|
||||
icon: '/icons/icon-192x192.webp'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error in push event:', error);
|
||||
self.registration.showNotification('New Notification', {
|
||||
body: 'You have a new update.'
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -1,3 +1,4 @@
|
||||
//server.js
|
||||
require('dotenv').config();
|
||||
console.log('--- DIAGNOSTIC TEST ---');
|
||||
console.log('PUBLIC_VAPID_KEY loaded as:', process.env.PUBLIC_VAPID_KEY);
|
||||
@ -17,7 +18,7 @@ const PORT = process.env.PORT || 3000;
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'default_secret_key';
|
||||
const ADMIN_USERNAME = process.env.ADMIN_USERNAME || 'admin';
|
||||
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'adminpassword';
|
||||
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU';
|
||||
const publicVapidKey = process.env.PUBLIC_VAPID_KEY;
|
||||
const privateVapidKey = process.env.PRIVATE_VAPID_KEY;
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user