From 58cf736e825e2756aaefe6aa12d25cbbabf86b70 Mon Sep 17 00:00:00 2001 From: kyuuseiryuu Date: Tue, 24 Mar 2026 17:33:52 +0900 Subject: [PATCH] feat(notification): add click-to-open functionality and cleanup test notification - Update useFirebaseNotificationProcessor to handle notification clicks by opening the URL from data.url. - Implement notificationclick event handler in sw.ts to redirect users when a notification is clicked. - Simplify the registration success notification in index.tsx by removing the unnecessary JSON body argument. - Ensure seamless navigation when users interact with push notifications by passing the URL through the notification payload. --- src/hooks/useFirebaseNotificationProcessor.ts | 4 ++++ src/index.tsx | 2 +- src/sw.ts | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) 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