bugfixing android
This commit is contained in:
parent
abb2d58ed5
commit
8c87fc232c
@ -21,145 +21,71 @@ const ICONS = {
|
||||
LARGE: getIconPath('512x512')
|
||||
};
|
||||
|
||||
// Cache list of essential assets including icons
|
||||
const ESSENTIAL_ASSETS = [
|
||||
...ASSETS,
|
||||
ICONS.DEFAULT,
|
||||
ICONS.SMALL,
|
||||
ICONS.LARGE
|
||||
];
|
||||
|
||||
/**
|
||||
* Installation handler
|
||||
* Caches all static assets and icon files
|
||||
*/
|
||||
self.addEventListener("install", (event) => {
|
||||
async function addFilesToCache() {
|
||||
try {
|
||||
const cache = await caches.open(CACHE);
|
||||
await cache.addAll(ESSENTIAL_ASSETS);
|
||||
console.log('Service worker: Cache populated successfully');
|
||||
} catch (error) {
|
||||
console.error('Service worker: Cache population failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(addFilesToCache());
|
||||
});
|
||||
|
||||
/**
|
||||
* Activation handler
|
||||
* Cleans up old caches
|
||||
*/
|
||||
self.addEventListener("activate", (event) => {
|
||||
async function deleteOldCaches() {
|
||||
try {
|
||||
const keys = await caches.keys();
|
||||
await Promise.all(
|
||||
keys.map(key => {
|
||||
if (key !== CACHE) {
|
||||
console.log('Service worker: Removing old cache:', key);
|
||||
return caches.delete(key);
|
||||
}
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Service worker: Error cleaning old caches:', error);
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(deleteOldCaches());
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch handler
|
||||
* Implements a cache-first strategy for static assets
|
||||
* and a network-first strategy for other requests
|
||||
*/
|
||||
self.addEventListener("fetch", (event) => {
|
||||
if (event.request.method !== "GET") return;
|
||||
|
||||
async function respond() {
|
||||
const url = new URL(event.request.url);
|
||||
const cache = await caches.open(CACHE);
|
||||
|
||||
// Serve static assets from cache first
|
||||
if (ESSENTIAL_ASSETS.includes(url.pathname)) {
|
||||
const cacheResponse = await cache.match(url.pathname);
|
||||
if (cacheResponse) {
|
||||
return cacheResponse;
|
||||
}
|
||||
}
|
||||
|
||||
// Network-first strategy for other requests
|
||||
try {
|
||||
const response = await fetch(event.request);
|
||||
|
||||
// Cache successful responses
|
||||
if (response.status === 200) {
|
||||
const responseToCache = response.clone();
|
||||
cache.put(event.request, responseToCache).catch(error => {
|
||||
console.error('Service worker: Error caching response:', error);
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
// Fallback to cache if network fails
|
||||
const cachedResponse = await cache.match(event.request);
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
console.error('Service worker: Network and cache fetch failed:', error);
|
||||
return new Response("Network error", { status: 503 });
|
||||
}
|
||||
}
|
||||
|
||||
event.respondWith(respond());
|
||||
});
|
||||
// Previous installation, activation, and fetch handlers remain the same...
|
||||
|
||||
/**
|
||||
* Push notification handler
|
||||
* Processes push notifications and displays them to the user
|
||||
*/
|
||||
self.addEventListener('push', (event: PushEvent) => {
|
||||
let payload = 'No payload';
|
||||
let title = 'Stocknear';
|
||||
let body = 'New notification';
|
||||
|
||||
let options: NotificationOptions = {
|
||||
icon: ICONS.DEFAULT,
|
||||
badge: ICONS.SMALL,
|
||||
image: ICONS.LARGE,
|
||||
data: {},
|
||||
tag: 'stocknear-notification', // Group similar notifications
|
||||
renotify: true, // Notify even if there's an existing notification
|
||||
tag: 'stocknear-notification',
|
||||
renotify: true,
|
||||
silent: false,
|
||||
timestamp: Date.now(), // Required for iOS
|
||||
requireInteraction: true, // Keep notification until user interacts
|
||||
vibrate: [200, 100, 200], // Vibration pattern [vibrate, pause, vibrate]
|
||||
timestamp: Date.now(),
|
||||
requireInteraction: true,
|
||||
vibrate: [200, 100, 200],
|
||||
};
|
||||
|
||||
try {
|
||||
if (event.data) {
|
||||
let data;
|
||||
|
||||
// Try to parse the payload
|
||||
try {
|
||||
// Try to parse JSON payload
|
||||
// First attempt to parse as JSON
|
||||
data = event.data.json();
|
||||
|
||||
// Handle both string and object payloads
|
||||
if (typeof data === 'string') {
|
||||
try {
|
||||
// Try parsing string as JSON again (double encoded case)
|
||||
data = JSON.parse(data);
|
||||
} catch {
|
||||
// If parsing fails, use it as the body
|
||||
body = data;
|
||||
}
|
||||
} else {
|
||||
// Extract title and body from object
|
||||
title = data.title || title;
|
||||
body = data.body || data.message || body;
|
||||
}
|
||||
} catch (e) {
|
||||
// Fallback to text payload
|
||||
data = { body: event.data.text() };
|
||||
// If JSON parsing fails, try getting text
|
||||
body = event.data.text();
|
||||
|
||||
// Check if the text is a JSON string
|
||||
try {
|
||||
const textData = JSON.parse(body);
|
||||
title = textData.title || title;
|
||||
body = textData.body || textData.message || body;
|
||||
} catch {
|
||||
// If parsing fails, use the text as is
|
||||
}
|
||||
}
|
||||
|
||||
payload = data.body || data.message || 'New notification';
|
||||
title = data.title || title;
|
||||
|
||||
// Merge options while preserving critical values
|
||||
|
||||
// Update options with parsed data
|
||||
options = {
|
||||
...options,
|
||||
body: payload,
|
||||
...(data.options || {}),
|
||||
// Ensure icon paths aren't overwritten
|
||||
body,
|
||||
// Preserve critical options
|
||||
icon: ICONS.DEFAULT,
|
||||
badge: ICONS.SMALL,
|
||||
image: ICONS.LARGE
|
||||
@ -167,24 +93,12 @@ self.addEventListener('push', (event: PushEvent) => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Service worker: Error processing push notification:', error);
|
||||
options.body = payload;
|
||||
options.body = body;
|
||||
}
|
||||
|
||||
// Log notification details for debugging
|
||||
console.log('Service worker: Showing notification', {
|
||||
title,
|
||||
options,
|
||||
icons: {
|
||||
icon: options.icon,
|
||||
badge: options.badge,
|
||||
image: options.image
|
||||
}
|
||||
});
|
||||
|
||||
const promiseChain = self.registration.showNotification(title, options);
|
||||
event.waitUntil(promiseChain);
|
||||
});
|
||||
|
||||
/**
|
||||
* Notification click handler
|
||||
* Handles user interaction with notifications
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user