54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
/* eslint-disable import/no-named-as-default-member */
|
|
import webpush, { WebPushError } from 'web-push'
|
|
|
|
import { getServerEnvVariable } from './serverEnvVariables'
|
|
|
|
const VAPID_PUBLIC_KEY = getServerEnvVariable('VAPID_PUBLIC_KEY')
|
|
const VAPID_PRIVATE_KEY = getServerEnvVariable('VAPID_PRIVATE_KEY')
|
|
const VAPID_SUBJECT = getServerEnvVariable('VAPID_SUBJECT')
|
|
|
|
// Configure VAPID keys
|
|
webpush.setVapidDetails(VAPID_SUBJECT, VAPID_PUBLIC_KEY, VAPID_PRIVATE_KEY)
|
|
|
|
export { webpush }
|
|
|
|
export type NotificationAction = {
|
|
action: string
|
|
title: string
|
|
icon?: string
|
|
|
|
url: string | null
|
|
iconName?: string
|
|
}
|
|
|
|
export type NotificationPayload = {
|
|
title: string
|
|
body: string | null
|
|
actions: NotificationAction[]
|
|
}
|
|
|
|
export async function sendPushNotification(
|
|
subscription: {
|
|
endpoint: string
|
|
keys: {
|
|
p256dh: string
|
|
auth: string
|
|
}
|
|
},
|
|
payload: NotificationPayload
|
|
) {
|
|
try {
|
|
// NOTE: View sw.js to see how the notification is handled
|
|
const result = await webpush.sendNotification(subscription, JSON.stringify(payload), {
|
|
TTL: 24 * 60 * 60, // 24 hours
|
|
})
|
|
return { success: true, result } as const
|
|
} catch (error) {
|
|
console.error('Error sending push notification:', error)
|
|
return {
|
|
success: false,
|
|
error: error instanceof WebPushError ? error : undefined,
|
|
} as const
|
|
}
|
|
}
|