3.0 KiB
3.0 KiB
Implementation Plan - PWA Push Notifications
This plan outlines the steps to implement Push Notifications for the Cat Sitting Planner PWA.
Goals
- Allow users to subscribe to Push Notifications from the browser.
- Send Push Notifications for existing events (New Booking, Cancellation, Completion, Instructions Update).
- Ensure integration with the Service Worker.
User Actions Required
- Generate VAPID Keys:
Run
npx web-push generate-vapid-keysin your terminal. Add the output to your.env(create if needed) or.env.local:Note:NEXT_PUBLIC_VAPID_PUBLIC_KEY="<your_public_key>" VAPID_PRIVATE_KEY="<your_private_key>" VAPID_SUBJECT="mailto:your-email@example.com"NEXT_PUBLIC_prefix is needed for the frontend to access the public key.
Steps
1. Dependencies and Configuration
- Install
web-push:npm install web-push && npm install -D @types/web-push - Install
serwistor ensurenext-pwacan handle custom service workers. (We will useimportScriptswithnext-pwa).
2. Database Schema
- Update
prisma/schema.prisma: AddPushSubscriptionmodel linked toPlan.model PushSubscription { id String @id @default(cuid()) planId String plan Plan @relation(fields: [planId], references: [id], onDelete: Cascade) endpoint String @unique p256dh String auth String createdAt DateTime @default(now()) } - Run
npx prisma migrate dev --name add_push_subscription
3. Service Worker
- Create
public/push-sw.js: Implement thepushevent listener to show notifications. Implement thenotificationclickevent listener to focus/open the app. - Update
next.config.mjs: Configurenext-pwa(Workbox) to importpush-sw.jsviaimportScripts.
4. Server Actions & Library
- Create
lib/push.ts: Helper functions to initializeweb-pushand send notifications. - Create
app/actions/subscription.ts:subscribeUser(planId, subscription): Saves subscription to DB.unsubscribeUser(endpoint): Removes subscription. - Update
lib/notifications.ts: AddsendPlanNotification(planId, message, webhookUrl?). This function will:- Send to Webhook (if exists).
- Fetch all subscriptions for
planId. - Send Web Push to all.
- Handle 410 (Gone) by deleting sub.
5. Backend Integration
- Update
app/actions/booking.ts: ReplacesendNotificationwithsendPlanNotification. - Update
app/actions/plan.ts: ReplacesendNotificationwithsendPlanNotification.
6. Frontend Integration
- Create
components/push-notification-manager.tsx: UI component to check permission, register SW (if not handled), and subscribe. Show "Enable Notifications" button. - Add
PushNotificationManagertoDashboardorSettings.
Verification
- Test on Localhost (requires HTTPS or localhost exception).
- Verify notifications appear for:
- Booking creation
- Booking completion
- Cancellation
- Instructions update