bugfixing android
This commit is contained in:
parent
8c87fc232c
commit
07481735d0
@ -13,7 +13,7 @@ webPush.setVapidDetails(
|
|||||||
|
|
||||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||||
const { pb, apiKey } = locals;
|
const { pb, apiKey } = locals;
|
||||||
const { title, body, key, options = {} } = await request?.json();
|
const { title, body, key } = await request?.json();
|
||||||
|
|
||||||
if (apiKey !== key) {
|
if (apiKey !== key) {
|
||||||
console.warn('Invalid API key');
|
console.warn('Invalid API key');
|
||||||
@ -37,19 +37,9 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isApple = subscriptionData.endpoint.includes('web.push.apple.com');
|
// Send just the body text for Android
|
||||||
const payload = JSON.stringify({
|
const isAndroid = !subscriptionData.endpoint.includes('web.push.apple.com');
|
||||||
title,
|
const payload = isAndroid ? body : JSON.stringify({ title, body });
|
||||||
body,
|
|
||||||
options: {
|
|
||||||
...options,
|
|
||||||
// Additional options specific to the platform
|
|
||||||
...(isApple && {
|
|
||||||
silent: false, // Ensure notification shows on iOS
|
|
||||||
timestamp: Date.now(), // Required for iOS
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await webPush.sendNotification(subscriptionData, payload);
|
await webPush.sendNotification(subscriptionData, payload);
|
||||||
console.log(`Notification sent to: ${subscriptionData.endpoint}`);
|
console.log(`Notification sent to: ${subscriptionData.endpoint}`);
|
||||||
|
|||||||
@ -5,99 +5,63 @@ declare let self: ServiceWorkerGlobalScope;
|
|||||||
|
|
||||||
import { build, files, version } from "$service-worker";
|
import { build, files, version } from "$service-worker";
|
||||||
|
|
||||||
// Constants
|
|
||||||
const CACHE = `cache-${version}`;
|
const CACHE = `cache-${version}`;
|
||||||
const ASSETS = [...build, ...files];
|
const ASSETS = [...build, ...files];
|
||||||
|
|
||||||
// Helper function to resolve icon paths
|
|
||||||
function getIconPath(size: string) {
|
function getIconPath(size: string) {
|
||||||
return new URL(`/pwa-${size}.png`, self.location.origin).href;
|
return new URL(`/pwa-${size}.png`, self.location.origin).href;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Icon configurations
|
|
||||||
const ICONS = {
|
const ICONS = {
|
||||||
DEFAULT: getIconPath('192x192'),
|
DEFAULT: getIconPath('192x192'),
|
||||||
SMALL: getIconPath('64x64'),
|
SMALL: getIconPath('64x64'),
|
||||||
LARGE: getIconPath('512x512')
|
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) => {
|
self.addEventListener('push', (event: PushEvent) => {
|
||||||
|
if (!event.data) return;
|
||||||
|
|
||||||
let title = 'Stocknear';
|
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 {
|
try {
|
||||||
if (event.data) {
|
// Try to get the payload as text first
|
||||||
let data;
|
const payload = event.data.text();
|
||||||
|
|
||||||
// Try to parse the payload
|
try {
|
||||||
try {
|
// Try to parse as JSON
|
||||||
// First attempt to parse as JSON
|
const jsonData = JSON.parse(payload);
|
||||||
data = event.data.json();
|
if (jsonData.title) {
|
||||||
|
title = jsonData.title;
|
||||||
// Handle both string and object payloads
|
body = jsonData.body;
|
||||||
if (typeof data === 'string') {
|
} else {
|
||||||
try {
|
// If no title in JSON, use the entire payload as body
|
||||||
// Try parsing string as JSON again (double encoded case)
|
body = payload;
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
// Update options with parsed data
|
// If JSON parsing fails, use the payload as body
|
||||||
options = {
|
body = payload;
|
||||||
...options,
|
|
||||||
body,
|
|
||||||
// Preserve critical options
|
|
||||||
icon: ICONS.DEFAULT,
|
|
||||||
badge: ICONS.SMALL,
|
|
||||||
image: ICONS.LARGE
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.error('Service worker: Error processing push notification:', error);
|
body = 'New notification';
|
||||||
options.body = body;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const promiseChain = self.registration.showNotification(title, options);
|
const options: NotificationOptions = {
|
||||||
event.waitUntil(promiseChain);
|
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
|
* Notification click handler
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user