diff --git a/src/hooks/useFirebaseNotificationProcessor.ts b/src/hooks/useFirebaseNotificationProcessor.ts index 564495d..653c1ee 100644 --- a/src/hooks/useFirebaseNotificationProcessor.ts +++ b/src/hooks/useFirebaseNotificationProcessor.ts @@ -16,6 +16,10 @@ export const useFirebaseNotificationProcessor = () => { title: data.title, description: data.body, icon: data.icon, + onClick: () => { + if (!data.url) return; + open(data.url, '_blank'); // 打开通知 + }, }); }); diff --git a/src/index.tsx b/src/index.tsx index d2d7ecc..cc33c5c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -92,7 +92,7 @@ const server = Bun.serve({ const [hasOldToken] = await Promise.all([ prisma.notificationToken.count({ where }).then(num => num > 0), ]); - await sendNotification(token, { title: '通知已注册!', body: JSON.stringify({ hasOldToken }) }); + await sendNotification(token, { title: '通知已注册!' }); if (hasOldToken) { return Response.json({ success: true, diff --git a/src/sw.ts b/src/sw.ts index af2fd04..3787191 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -37,4 +37,10 @@ onBackgroundMessage(messaging, (payload: MessagePayload) => { self.registration.showNotification(notificationTitle, notificationOptions); }); +self.addEventListener('notificationclick', ev => { + const url = ev.notification.data.url; + if (!url) return; + open(url); +}); + console.log('sw', self); \ No newline at end of file