feat: 添加推送通知功能,支持Web Push订阅和发送

This commit is contained in:
2025-11-28 22:35:45 +08:00
parent a4bb8e984f
commit 35a948c515
13 changed files with 338 additions and 2 deletions
+30
View File
@@ -0,0 +1,30 @@
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function POST(request: Request) {
try {
const subscription = await request.json();
if (!subscription || !subscription.endpoint || !subscription.keys) {
return NextResponse.json({ error: 'Invalid subscription object' }, { status: 400 });
}
await prisma.pushSubscription.upsert({
where: { endpoint: subscription.endpoint },
update: {
p256dh: subscription.keys.p256dh,
auth: subscription.keys.auth,
},
create: {
endpoint: subscription.endpoint,
p256dh: subscription.keys.p256dh,
auth: subscription.keys.auth,
},
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('Error saving subscription:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}