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
|
// This function handles the actual subscription process
|
||||||
async function subscribeToNotifications() {
|
async function subscribeToNotifications() {
|
||||||
try {
|
try {
|
||||||
const publicVapidKey = process.env.PUBLIC_VAPID_KEY; // Make sure this is set in your .env
|
// CORRECT
|
||||||
const token = localStorage.getItem('authToken');
|
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU'; const token = localStorage.getItem('authToken');
|
||||||
|
|
||||||
if (!token || !publicVapidKey) {
|
if (!token || !publicVapidKey) {
|
||||||
return console.error('Auth token or VAPID key is missing.');
|
return console.error('Auth token or VAPID key is missing.');
|
||||||
|
|||||||
71
public/sw.js
71
public/sw.js
@ -1,73 +1,54 @@
|
|||||||
// --- Time Pulse Service Worker ---
|
// public/sw.js - CORRECTED
|
||||||
|
|
||||||
const CACHE_NAME = 'timepulse-v1';
|
const CACHE_NAME = 'timepulse-v1';
|
||||||
|
|
||||||
// Files to cache (App Shell)
|
|
||||||
const filesToCache = [
|
const filesToCache = [
|
||||||
'/',
|
'/', '/index.html', '/style/style.css', '/js/main.js', '/js/ui.js',
|
||||||
'/index.html',
|
'/js/api.js', '/js/utils.js', '/manifest.json',
|
||||||
'/style/style.css',
|
'/icons/icon-192x192.webp', '/icons/icon-512x512.webp'
|
||||||
'/js/main.js',
|
|
||||||
'/js/ui.js',
|
|
||||||
'/js/api.js',
|
|
||||||
'/js/utils.js',
|
|
||||||
'/manifest.json',
|
|
||||||
'/icons/icon-192x192.webp',
|
|
||||||
'/icons/icon-512x512.webp'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// --- Install Event ---
|
// --- Install Event ---
|
||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', event => {
|
||||||
console.log('[SW] Install event');
|
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(filesToCache)));
|
||||||
event.waitUntil(
|
self.skipWaiting();
|
||||||
caches.open(CACHE_NAME)
|
|
||||||
.then(cache => {
|
|
||||||
console.log('[SW] Caching app shell');
|
|
||||||
return cache.addAll(filesToCache);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
self.skipWaiting(); // Activate worker immediately
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Activate Event ---
|
// --- Activate Event ---
|
||||||
self.addEventListener('activate', event => {
|
self.addEventListener('activate', event => {
|
||||||
console.log('[SW] Activate event');
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then(keys =>
|
caches.keys().then(keys => Promise.all(
|
||||||
Promise.all(
|
|
||||||
keys.map(key => {
|
keys.map(key => {
|
||||||
if (key !== CACHE_NAME) {
|
if (key !== CACHE_NAME) {
|
||||||
console.log('[SW] Removing old cache:', key);
|
|
||||||
return caches.delete(key);
|
return caches.delete(key);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
))
|
||||||
)
|
|
||||||
);
|
);
|
||||||
self.clients.claim(); // Become available to all pages
|
self.clients.claim();
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Fetch Event ---
|
// --- Fetch Event ---
|
||||||
self.addEventListener('fetch', event => {
|
self.addEventListener('fetch', event => {
|
||||||
// Ignore non-GET requests and API calls
|
|
||||||
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(event.request)
|
caches.match(event.request).then(response => response || fetch(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
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- 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();
|
require('dotenv').config();
|
||||||
console.log('--- DIAGNOSTIC TEST ---');
|
console.log('--- DIAGNOSTIC TEST ---');
|
||||||
console.log('PUBLIC_VAPID_KEY loaded as:', process.env.PUBLIC_VAPID_KEY);
|
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 JWT_SECRET = process.env.JWT_SECRET || 'default_secret_key';
|
||||||
const ADMIN_USERNAME = process.env.ADMIN_USERNAME || 'admin';
|
const ADMIN_USERNAME = process.env.ADMIN_USERNAME || 'admin';
|
||||||
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'adminpassword';
|
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 privateVapidKey = process.env.PRIVATE_VAPID_KEY;
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user