138 lines
3.9 KiB
JavaScript
138 lines
3.9 KiB
JavaScript
// Service Worker für iPhone Notifications
|
|
const CACHE_NAME = 'ninjacross-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/test-push.html',
|
|
'/sw.js'
|
|
];
|
|
|
|
// Install event
|
|
self.addEventListener('install', function(event) {
|
|
console.log('Service Worker installing...');
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(function(cache) {
|
|
// Add files one by one to handle failures gracefully
|
|
return Promise.allSettled(
|
|
urlsToCache.map(url =>
|
|
cache.add(url).catch(err => {
|
|
console.warn(`Failed to cache ${url}:`, err);
|
|
return null; // Continue with other files
|
|
})
|
|
)
|
|
);
|
|
})
|
|
.then(() => {
|
|
console.log('Service Worker installation completed');
|
|
// Skip waiting to activate immediately
|
|
return self.skipWaiting();
|
|
})
|
|
.catch(err => {
|
|
console.error('Service Worker installation failed:', err);
|
|
// Still try to skip waiting
|
|
return self.skipWaiting();
|
|
})
|
|
);
|
|
});
|
|
|
|
// Activate event
|
|
self.addEventListener('activate', function(event) {
|
|
console.log('Service Worker activating...');
|
|
event.waitUntil(
|
|
caches.keys().then(function(cacheNames) {
|
|
return Promise.all(
|
|
cacheNames.map(function(cacheName) {
|
|
if (cacheName !== CACHE_NAME) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
}).then(() => {
|
|
// Take control of all clients immediately
|
|
return self.clients.claim();
|
|
})
|
|
);
|
|
});
|
|
|
|
// Listen for skip waiting message
|
|
self.addEventListener('message', function(event) {
|
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|
|
|
|
// Fetch event
|
|
self.addEventListener('fetch', function(event) {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(function(response) {
|
|
// Return cached version or fetch from network
|
|
return response || fetch(event.request);
|
|
}
|
|
)
|
|
);
|
|
});
|
|
|
|
// Push event (für iPhone Notifications)
|
|
self.addEventListener('push', function(event) {
|
|
console.log('Push received:', event);
|
|
|
|
const options = {
|
|
body: 'Du hast eine neue Notification!',
|
|
icon: '/pictures/icon-192.png',
|
|
badge: '/pictures/icon-192.png',
|
|
vibrate: [100, 50, 100],
|
|
data: {
|
|
dateOfArrival: Date.now(),
|
|
primaryKey: 1
|
|
},
|
|
actions: [
|
|
{
|
|
action: 'explore',
|
|
title: 'Dashboard öffnen',
|
|
icon: '/pictures/icon-192.png'
|
|
},
|
|
{
|
|
action: 'close',
|
|
title: 'Schließen',
|
|
icon: '/pictures/icon-192.png'
|
|
}
|
|
]
|
|
};
|
|
|
|
if (event.data) {
|
|
const data = event.data.json();
|
|
options.body = data.body || options.body;
|
|
options.title = data.title || 'Ninja Cross';
|
|
}
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification('Ninja Cross', options)
|
|
);
|
|
});
|
|
|
|
// Notification click event
|
|
self.addEventListener('notificationclick', function(event) {
|
|
console.log('Notification clicked:', event);
|
|
|
|
event.notification.close();
|
|
|
|
if (event.action === 'explore') {
|
|
event.waitUntil(
|
|
clients.openWindow('/dashboard.html')
|
|
);
|
|
}
|
|
});
|
|
|
|
// Background sync (für offline functionality)
|
|
self.addEventListener('sync', function(event) {
|
|
if (event.tag === 'background-sync') {
|
|
event.waitUntil(doBackgroundSync());
|
|
}
|
|
});
|
|
|
|
async function doBackgroundSync() {
|
|
// Sync data when back online
|
|
console.log('Background sync triggered');
|
|
}
|