@@ -6,6 +6,14 @@ import { messaging } from './firebase' // Adjust as needed
6
6
import { Subscription } from '@prisma/client'
7
7
import { JsonValue } from '@prisma/client/runtime/library'
8
8
import prisma from '@/lib/db'
9
+ import { logger } from '@/lib/logger'
10
+
11
+ // Initialize VAPID keys for web push notifications
12
+ webpush . setVapidDetails (
13
+ 'mailto:your-email@example.com' ,
14
+ process . env . NEXT_PUBLIC_VAPID_PUBLIC_KEY ! ,
15
+ process . env . VAPID_PRIVATE_KEY !
16
+ )
9
17
10
18
/**
11
19
* Sends a push notification to a subscription using either web push (VAPID) or FCM.
@@ -22,7 +30,7 @@ export async function sendNotification(
22
30
notificationPayload : NotificationPayload
23
31
) : Promise < void > {
24
32
try {
25
- console . log ( 'Sending notification to subscription:' , subscription )
33
+ logger . info ( 'Sending notification to subscription:' , subscription )
26
34
27
35
// Handle browser-based web push notifications.
28
36
if ( subscription . type === 'web' ) {
@@ -82,7 +90,7 @@ export async function sendNotification(
82
90
83
91
// Send the FCM message using Firebase.
84
92
const response = await messaging . send ( fcmMessage )
85
- console . log ( 'FCM response:' , response )
93
+ logger . info ( 'FCM response:' , response )
86
94
}
87
95
} catch ( error ) {
88
96
/**
@@ -93,9 +101,9 @@ export async function sendNotification(
93
101
// Status code 410 indicates that the subscription is no longer valid.
94
102
if ( error . statusCode === 410 ) {
95
103
await prisma . subscription . delete ( { where : { id : subscription . id } } )
96
- console . log ( `Subscription with id ${ subscription . id } removed due to expiration.` )
104
+ logger . info ( `Subscription with id ${ subscription . id } removed due to expiration.` )
97
105
} else {
98
- console . log (
106
+ logger . info (
99
107
`Failed to send notification to subscription id ${ subscription . id } :` ,
100
108
error . statusCode ,
101
109
error . body
@@ -108,12 +116,12 @@ export async function sendNotification(
108
116
error . code === 'messaging/registration-token-not-registered'
109
117
) {
110
118
await prisma . subscription . delete ( { where : { id : subscription . id } } )
111
- console . log ( `Removed invalid FCM token for subscription id ${ subscription . id } .` )
119
+ logger . info ( `Removed invalid FCM token for subscription id ${ subscription . id } .` )
112
120
} else {
113
- console . log ( `Failed to send FCM notification to subscription id ${ subscription . id } :` , error )
121
+ logger . info ( `Failed to send FCM notification to subscription id ${ subscription . id } :` , error )
114
122
}
115
123
} else {
116
- console . log (
124
+ logger . info (
117
125
`An error occurred while sending notification to subscription id ${ subscription . id } :` ,
118
126
error
119
127
)
0 commit comments