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.
This commit is contained in:
kyuuseiryuu 2026-03-24 17:33:52 +09:00
parent 2774037012
commit 58cf736e82
3 changed files with 11 additions and 1 deletions

View File

@ -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'); // 打开通知
},
});
});

View File

@ -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,

View File

@ -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);