bugfixing android

This commit is contained in:
MuslemRahimi 2025-02-02 23:12:40 +01:00
parent abb2d58ed5
commit 8c87fc232c

View File

@ -21,145 +21,71 @@ const ICONS = {
LARGE: getIconPath('512x512') LARGE: getIconPath('512x512')
}; };
// Cache list of essential assets including icons // Previous installation, activation, and fetch handlers remain the same...
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());
});
/** /**
* Push notification handler * Push notification handler
* Processes push notifications and displays them to the user * Processes push notifications and displays them to the user
*/ */
self.addEventListener('push', (event: PushEvent) => { self.addEventListener('push', (event: PushEvent) => {
let payload = 'No payload';
let title = 'Stocknear'; let title = 'Stocknear';
let body = 'New notification';
let options: NotificationOptions = { let options: NotificationOptions = {
icon: ICONS.DEFAULT, icon: ICONS.DEFAULT,
badge: ICONS.SMALL, badge: ICONS.SMALL,
image: ICONS.LARGE, image: ICONS.LARGE,
data: {}, data: {},
tag: 'stocknear-notification', // Group similar notifications tag: 'stocknear-notification',
renotify: true, // Notify even if there's an existing notification renotify: true,
silent: false, silent: false,
timestamp: Date.now(), // Required for iOS timestamp: Date.now(),
requireInteraction: true, // Keep notification until user interacts requireInteraction: true,
vibrate: [200, 100, 200], // Vibration pattern [vibrate, pause, vibrate] vibrate: [200, 100, 200],
}; };
try { try {
if (event.data) { if (event.data) {
let data; let data;
// Try to parse the payload
try { try {
// Try to parse JSON payload // First attempt to parse as JSON
data = event.data.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) { } catch (e) {
// Fallback to text payload // If JSON parsing fails, try getting text
data = { body: event.data.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'; // Update options with parsed data
title = data.title || title;
// Merge options while preserving critical values
options = { options = {
...options, ...options,
body: payload, body,
...(data.options || {}), // Preserve critical options
// Ensure icon paths aren't overwritten
icon: ICONS.DEFAULT, icon: ICONS.DEFAULT,
badge: ICONS.SMALL, badge: ICONS.SMALL,
image: ICONS.LARGE image: ICONS.LARGE
@ -167,24 +93,12 @@ self.addEventListener('push', (event: PushEvent) => {
} }
} catch (error) { } catch (error) {
console.error('Service worker: Error processing push notification:', 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); const promiseChain = self.registration.showNotification(title, options);
event.waitUntil(promiseChain); event.waitUntil(promiseChain);
}); });
/** /**
* Notification click handler * Notification click handler
* Handles user interaction with notifications * Handles user interaction with notifications