update push notification
This commit is contained in:
parent
7270087a17
commit
dbac266d23
@ -62,7 +62,6 @@
|
||||
description="Stocknear has everything you need to analyze stocks with help of AI, including detailed financial data, statistics, news and charts."
|
||||
image=""
|
||||
/>
|
||||
/>
|
||||
|
||||
<div
|
||||
class="w-full sm:max-w-[1400px] overflow-hidden m-auto min-h-screen bg-default mb-40"
|
||||
|
||||
59
src/routes/api/sendPushSubscription/+server.ts
Normal file
59
src/routes/api/sendPushSubscription/+server.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import webPush from 'web-push';
|
||||
|
||||
const VAPID_PUBLIC_KEY = import.meta.env.VITE_VAPID_PUBLIC_KEY;
|
||||
const VAPID_PRIVATE_KEY = import.meta.env.VITE_VAPID_PRIVATE_KEY;
|
||||
|
||||
webPush.setVapidDetails(
|
||||
'mailto:contact@stocknear.com',
|
||||
VAPID_PUBLIC_KEY,
|
||||
VAPID_PRIVATE_KEY
|
||||
);
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const { pb, apiKey } = locals;
|
||||
|
||||
const { title, body, key } = await request?.json();
|
||||
|
||||
if (apiKey === key) {
|
||||
|
||||
try {
|
||||
|
||||
console.log(title, body)
|
||||
|
||||
// Get all push subscriptions
|
||||
const subscriptions = await pb.collection('pushSubscription').getFullList({
|
||||
sort: '-created'
|
||||
});
|
||||
|
||||
// Send notifications to all subscriptions
|
||||
const sendNotifications = subscriptions?.map(async (subRecord) => {
|
||||
try {
|
||||
const subscriptionData = subRecord.subscription?.subscription;
|
||||
await webPush.sendNotification(
|
||||
subscriptionData, // Ensure correct format
|
||||
body
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error('Error sending notification:', error);
|
||||
|
||||
// Delete invalid subscriptions (410 means "Gone")
|
||||
if (error.statusCode === 410) {
|
||||
await pb.collection('pushSubscription').delete(subRecord.id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(sendNotifications);
|
||||
|
||||
return new Response(JSON.stringify({ success: true, message: `Notifications sent to ${subscriptions.length} devices` }));
|
||||
} catch (error: any) {
|
||||
console.error('Error sending notifications:', error);
|
||||
return new Response(JSON.stringify({ success: false, error: error.message }, { status: 500 }));
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('api key wrong')
|
||||
}
|
||||
|
||||
};
|
||||
@ -85,7 +85,8 @@ self.addEventListener('push', function (event: any) {
|
||||
const registration = (self as any).registration as ServiceWorkerRegistration;
|
||||
event.waitUntil(
|
||||
registration.showNotification('Stocknear', {
|
||||
body: payload
|
||||
body: payload,
|
||||
icon: "/pwa-192x192.png",
|
||||
})
|
||||
);
|
||||
} as EventListener);
|
||||
Loading…
x
Reference in New Issue
Block a user