From 07481735d073020544a219f52056b4d48eb01b88 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 2 Feb 2025 23:16:59 +0100 Subject: [PATCH] bugfixing android --- .../api/sendPushSubscription/+server.ts | 18 +-- src/service-worker.ts | 106 ++++++------------ 2 files changed, 39 insertions(+), 85 deletions(-) diff --git a/src/routes/api/sendPushSubscription/+server.ts b/src/routes/api/sendPushSubscription/+server.ts index 3c1e7ead..bbb5a005 100644 --- a/src/routes/api/sendPushSubscription/+server.ts +++ b/src/routes/api/sendPushSubscription/+server.ts @@ -13,7 +13,7 @@ webPush.setVapidDetails( export const POST: RequestHandler = async ({ request, locals }) => { const { pb, apiKey } = locals; - const { title, body, key, options = {} } = await request?.json(); + const { title, body, key } = await request?.json(); if (apiKey !== key) { console.warn('Invalid API key'); @@ -37,19 +37,9 @@ export const POST: RequestHandler = async ({ request, locals }) => { return; } - const isApple = subscriptionData.endpoint.includes('web.push.apple.com'); - const payload = JSON.stringify({ - title, - body, - options: { - ...options, - // Additional options specific to the platform - ...(isApple && { - silent: false, // Ensure notification shows on iOS - timestamp: Date.now(), // Required for iOS - }) - } - }); + // Send just the body text for Android + const isAndroid = !subscriptionData.endpoint.includes('web.push.apple.com'); + const payload = isAndroid ? body : JSON.stringify({ title, body }); await webPush.sendNotification(subscriptionData, payload); console.log(`Notification sent to: ${subscriptionData.endpoint}`); diff --git a/src/service-worker.ts b/src/service-worker.ts index 6f7c50b1..bb6c5096 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -5,99 +5,63 @@ declare let self: ServiceWorkerGlobalScope; import { build, files, version } from "$service-worker"; -// Constants const CACHE = `cache-${version}`; const ASSETS = [...build, ...files]; -// Helper function to resolve icon paths function getIconPath(size: string) { return new URL(`/pwa-${size}.png`, self.location.origin).href; } -// Icon configurations const ICONS = { DEFAULT: getIconPath('192x192'), SMALL: getIconPath('64x64'), LARGE: getIconPath('512x512') }; -// Previous installation, activation, and fetch handlers remain the same... +// Previous cache-related event listeners remain the same... -/** - * Push notification handler - * Processes push notifications and displays them to the user - */ self.addEventListener('push', (event: PushEvent) => { + if (!event.data) return; + let title = 'Stocknear'; - let body = 'New notification'; + let body: string; - let options: NotificationOptions = { - icon: ICONS.DEFAULT, - badge: ICONS.SMALL, - image: ICONS.LARGE, - data: {}, - tag: 'stocknear-notification', - renotify: true, - silent: false, - timestamp: Date.now(), - requireInteraction: true, - vibrate: [200, 100, 200], - }; - try { - if (event.data) { - let data; - - // Try to parse the payload - try { - // 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) { - // 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 - } + // Try to get the payload as text first + const payload = event.data.text(); + + try { + // Try to parse as JSON + const jsonData = JSON.parse(payload); + if (jsonData.title) { + title = jsonData.title; + body = jsonData.body; + } else { + // If no title in JSON, use the entire payload as body + body = payload; } - - // Update options with parsed data - options = { - ...options, - body, - // Preserve critical options - icon: ICONS.DEFAULT, - badge: ICONS.SMALL, - image: ICONS.LARGE - }; + } catch { + // If JSON parsing fails, use the payload as body + body = payload; } - } catch (error) { - console.error('Service worker: Error processing push notification:', error); - options.body = body; + } catch { + body = 'New notification'; } - const promiseChain = self.registration.showNotification(title, options); - event.waitUntil(promiseChain); + const options: NotificationOptions = { + body, + icon: ICONS.DEFAULT, + badge: ICONS.SMALL, + timestamp: Date.now(), + requireInteraction: true, + tag: 'stocknear-notification', + renotify: true, + vibrate: [200, 100, 200] + }; + + event.waitUntil( + self.registration.showNotification(title, options) + ); }); /** * Notification click handler