chore: add debug logging for push notifications
This commit is contained in:
@@ -39,6 +39,7 @@ export async function sendPlanNotification(planId: string, message: string, webh
|
||||
});
|
||||
|
||||
if (subscriptions.length > 0) {
|
||||
console.log(`[Push] Found ${subscriptions.length} subscriptions for plan ${planId}`);
|
||||
const payload = {
|
||||
title: "Cat Sitting Planner",
|
||||
body: message,
|
||||
@@ -56,12 +57,24 @@ export async function sendPlanNotification(planId: string, message: string, webh
|
||||
|
||||
subscriptions.forEach(sub => {
|
||||
promises.push((async () => {
|
||||
const res = await sendPushNotification(sub, payload);
|
||||
if (!res.success && (res.statusCode === 410 || res.statusCode === 404)) {
|
||||
await prisma.pushSubscription.delete({ where: { id: sub.id } });
|
||||
try {
|
||||
console.log(`[Push] Sending to endpoint ${sub.endpoint.slice(0, 20)}...`);
|
||||
const res = await sendPushNotification(sub, payload);
|
||||
console.log(`[Push] Result for ${sub.endpoint.slice(0, 20)}...:`, res);
|
||||
if (!res.success) {
|
||||
console.error(`[Push] Failed for endpoint: ${res.statusCode}`);
|
||||
if (res.statusCode === 410 || res.statusCode === 404) {
|
||||
console.log(`[Push] Deleting stale subscription`);
|
||||
await prisma.pushSubscription.delete({ where: { id: sub.id } });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[Push] Error inside loop:`, err);
|
||||
}
|
||||
})());
|
||||
});
|
||||
} else {
|
||||
console.log(`[Push] No subscriptions found for plan ${planId}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch/send push subscriptions", e);
|
||||
|
||||
@@ -18,13 +18,15 @@ interface PushSubscriptionData {
|
||||
|
||||
export async function sendPushNotification(subscription: PushSubscriptionData, payload: any) {
|
||||
try {
|
||||
await webpush.sendNotification({
|
||||
console.log(`[PushLib] Sending to ${subscription.endpoint.slice(0, 30)}...`);
|
||||
const result = await webpush.sendNotification({
|
||||
endpoint: subscription.endpoint,
|
||||
keys: {
|
||||
p256dh: subscription.p256dh,
|
||||
auth: subscription.auth,
|
||||
}
|
||||
}, JSON.stringify(payload));
|
||||
console.log(`[PushLib] Success: ${result.statusCode}`);
|
||||
return { success: true, statusCode: 201 };
|
||||
} catch (error: any) {
|
||||
if (error.statusCode === 410 || error.statusCode === 404) {
|
||||
|
||||
Reference in New Issue
Block a user