bugfixing android
This commit is contained in:
parent
8c87fc232c
commit
07481735d0
@ -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}`);
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user