Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: web push test notifications

Blue Mousef73be56eaa2a
diff --git a/backend/apps/cloud/src/notification-channel/dispatchers/channel-dispatcher.service.ts b/backend/apps/cloud/src/notification-channel/dispatchers/channel-dispatcher.service.ts
index a55381653..74e0ddb41 100644
--- a/backend/apps/cloud/src/notification-channel/dispatchers/channel-dispatcher.service.ts
+++ b/backend/apps/cloud/src/notification-channel/dispatchers/channel-dispatcher.service.ts
@@ -44,6 +44,21 @@ export class ChannelDispatcherService {
return true
}
+ getDispatcher(type: NotificationChannelType): ChannelDispatcher | undefined {
+ return this.registry.get(type)
+ }
+
+ async sendOne(
+ channel: NotificationChannel,
+ message: RenderedAlertMessage,
+ ): Promise<void> {
+ const dispatcher = this.registry.get(channel.type)
+ if (!dispatcher) {
+ throw new Error(`No dispatcher registered for type ${channel.type}`)
+ }
+ await dispatcher.send(channel, message)
+ }
+
async dispatch(
channels: NotificationChannel[],
message: RenderedAlertMessage,
diff --git a/backend/apps/cloud/src/notification-channel/dispatchers/webpush-channel.service.ts b/backend/apps/cloud/src/notification-channel/dispatchers/webpush-channel.service.ts
index 916f5256b..5a42d5bf8 100644
--- a/backend/apps/cloud/src/notification-channel/dispatchers/webpush-channel.service.ts
+++ b/backend/apps/cloud/src/notification-channel/dispatchers/webpush-channel.service.ts
@@ -55,12 +55,20 @@ export class WebpushChannelService implements ChannelDispatcher {
channel: NotificationChannel,
message: RenderedAlertMessage,
): Promise<void> {
- if (!this.vapidConfigured) return
+ if (!this.vapidConfigured) {
+ throw new Error(
+ 'Web push is not configured on this server (missing or invalid VAPID keys).',
+ )
+ }
const cfg = channel.config as {
endpoint?: string
keys?: { p256dh: string; auth: string }
}
- if (!cfg?.endpoint || !cfg?.keys?.p256dh || !cfg?.keys?.auth) return
+ if (!cfg?.endpoint || !cfg?.keys?.p256dh || !cfg?.keys?.auth) {
+ throw new Error(
+ `Web push channel ${channel.id} is missing endpoint or keys.`,
+ )
+ }
const subscription: PushSubscription = {
endpoint: cfg.endpoint,
@@ -83,7 +91,7 @@ export class WebpushChannelService implements ChannelDispatcher {
try {
await webpush.sendNotification(subscription, payload)
} catch (reason: any) {
- // 404/410 mean the subscription is dead; keep alert links intact.
+ // 404/410 mean the subscription is dead; mark it as such and rethrow.
if (reason?.statusCode === 404 || reason?.statusCode === 410) {
this.logger.warn(
`Disabling expired webpush channel ${channel.id} (status ${reason.statusCode})`,
@@ -92,9 +100,18 @@ export class WebpushChannelService implements ChannelDispatcher {
isVerified: false,
disabledReason: `Expired: ${reason.statusCode} from WebPush server`,
})
- return
+ throw new Error(
+ `Push subscription is no longer valid (HTTP ${reason.statusCode}). The channel was disabled — re-enable browser notifications to subscribe again.`,
+ )
}
- this.logger.error(`Failed to send webpush: ${reason?.message || reason}`)
+ const detail =
+ reason?.body ||
+ reason?.message ||
+ (typeof reason === 'string' ? reason : 'Unknown error')
+ this.logger.error(
+ `Failed to send webpush to channel ${channel.id}: ${detail}`,
+ )
+ throw new Error(`Web push provider rejected the notification: ${detail}`)
}
}
}
diff --git a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
index 49131c55c..9d3a7d6f5 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
@@ -154,15 +154,17 @@ export class NotificationChannelController {
const channel = await this.channelService.findById(id)
if (!channel) throw new NotFoundException('Channel not found')
await this.channelService.ensureCallerCanManage(channel, userId)
- await this.dispatcherService.dispatch(
- [channel],
- {
+ try {
+ await this.dispatcherService.sendOne(channel, {
body: 'This is a test notification from Swetrix. Your channel is wired up correctly!',
subject: 'Swetrix test notification',
context: { test: true },
- },
- { ignoreVerification: true },
- )
+ })
+ } catch (reason: any) {
+ throw new BadRequestException(
+ `Failed to send test notification: ${reason?.message || reason}`,
+ )
+ }
return { ok: true }
}