- Add self-hosted push notifications using VAPID (no external services) - Create PushSubscription model in database - Implement server-side push logic with web-push library - Add notification helpers for list events (items, invitations, collaborators) - Create NotificationSettings UI component for profile page - Add usePushNotifications React hook for subscription management - Create API routes for subscription management and VAPID key - Update service worker with push event handlers - Integrate notifications into item actions (add, remove, complete) - Integrate notifications into sharing actions (invite, accept, remove) - Add VAPID key generation script - Add comprehensive documentation and setup guides Notifications are sent when: - Items are added/removed/completed in shared lists - Users are invited to lists - Invitations are accepted - Collaborators are removed from lists Works on iOS 16.4+ (when installed to home screen), Android, and desktop browsers.
5.8 KiB
5.8 KiB
✅ Push Notifications - FULLY IMPLEMENTED
What's Been Done
Your PWA now has a complete, self-hosted push notification system! Here's what was implemented:
1. Core Infrastructure ✅
- Database: Added
PushSubscriptionmodel to Prisma schema - Server Logic: Created
lib/push-notifications.tswith VAPID support - Notification Helpers: Created
lib/notification-helpers.tsfor event-specific notifications - API Routes: 3 routes for subscription management
- Service Worker: Updated with push event handlers
- React Hook: Created
usePushNotificationsfor client-side management - UI Component: Created
NotificationSettingscomponent
2. Integration Complete ✅
- Profile Page: Added NotificationSettings component
- Item Actions: Integrated notifications for:
- ✅ Item added →
notifyItemAdded() - ✅ Item removed →
notifyItemRemoved() - ✅ Item completed →
notifyItemCompleted()
- ✅ Item added →
- Sharing Actions: Integrated notifications for:
- ✅ List invitation →
notifyListInvitation() - ✅ Invitation accepted →
notifyInvitationAccepted() - ✅ Collaborator removed →
notifyRemovedFromList()
- ✅ List invitation →
3. Dependencies ✅
- Added
web-pushpackage - Added
@types/web-pushfor TypeScript - Added npm script:
npm run vapid:generate
Setup Instructions
Step 1: Install Dependencies
npm install
Step 2: Generate VAPID Keys
npm run vapid:generate
Copy the output to your .env file:
VAPID_PUBLIC_KEY=BNxxx...
VAPID_PRIVATE_KEY=xxx...
VAPID_SUBJECT=mailto:your-email@example.com
Step 3: Update Database
npx prisma db push
Step 4: Restart Your Server
npm run dev
How to Use
For Users:
- Go to Profile page
- Click "Enable" in the Notification Settings section
- Grant permission when browser prompts
- Done! You'll now receive notifications
Notifications Are Sent When:
- Someone adds an item to a shared list
- Someone removes an item from a shared list
- Someone completes an item in a shared list
- Someone invites you to a list
- Someone accepts your list invitation
- You're removed from a list
Technical Details
The "Server" Solution
You asked about the push notification server - it's your own Next.js app!
- Uses VAPID (Voluntary Application Server Identification)
- No external services needed (no Firebase, no third-party APIs)
- The
web-pushnpm package handles the protocol - Your VAPID keys authenticate your server to browsers
- Completely self-hosted and free
How It Works
- User enables notifications → Browser creates subscription
- Subscription saved to your database
- Event occurs (item added, etc.) → Your server action runs
- Server sends push via
web-pushlibrary - Browser's push service delivers to device
- Service worker displays notification
Browser Support
- ✅ Chrome/Edge (Desktop & Android)
- ✅ Firefox (Desktop & Android)
- ✅ Safari 16.4+ (macOS & iOS when installed to home screen)
Files Created
Core Files
lib/push-notifications.ts- Server push logiclib/notification-helpers.ts- Event-specific notificationshooks/usePushNotifications.ts- React hookcomponents/NotificationSettings.tsx- UI componentapp/api/push/vapid-key/route.ts- API routeapp/api/push/subscribe/route.ts- API routeapp/api/push/unsubscribe/route.ts- API routescripts/generate-vapid-keys.js- Key generation script
Documentation
PUSH_NOTIFICATIONS_QUICK_START.md- 5-minute setupPUSH_NOTIFICATIONS_COMPLETE.md- Full overviewPUSH_NOTIFICATIONS_SETUP.md- Detailed setupPUSH_NOTIFICATIONS_INTEGRATION_EXAMPLE.md- Code examplesPUSH_NOTIFICATIONS_GUIDE.md- Architecture guidePUSH_NOTIFICATIONS_IMPLEMENTED.md- This file
Modified Files
prisma/schema.prisma- Added PushSubscription modelpublic/sw.js- Added push event handlerspackage.json- Added dependencies and script.env.example- Added VAPID key placeholdersapp/profile/page.tsx- Added NotificationSettings componentlib/actions.ts- Integrated notification callslib/sharing-actions.ts- Integrated notification calls
Testing
Desktop (Chrome)
- Start dev server:
npm run dev - Go to profile, enable notifications
- Open another browser/incognito window
- Log in as different user
- Add item to shared list
- See notification in first browser
iOS (Real Device)
- Deploy with HTTPS
- Open in Safari
- Share → Add to Home Screen
- Open installed app
- Enable notifications in profile
- Test from another device
Production Checklist
- Run
npm install - Run
npm run vapid:generate - Add VAPID keys to production
.env - Run
npx prisma db pushon production database - Verify HTTPS is working
- Test notifications on actual devices
- Monitor logs for errors
What Makes This Special
- Self-Hosted: No external dependencies or costs
- Privacy-Focused: All data stays in your database
- Multi-Device: Users can subscribe on multiple devices
- Automatic Cleanup: Expired subscriptions removed automatically
- Non-Blocking: Notifications sent asynchronously
- Graceful Degradation: If notifications fail, actions still succeed
Next Steps (Optional Enhancements)
- Add per-list notification preferences
- Add quiet hours (don't notify during certain times)
- Add notification history/inbox
- Add action buttons to notifications
- Add notification grouping/batching
- Add sound customization
Support
All files compile without errors. The implementation is complete and ready to use!
For questions, refer to:
PUSH_NOTIFICATIONS_QUICK_START.mdfor setupPUSH_NOTIFICATIONS_INTEGRATION_EXAMPLE.mdfor code examplesPUSH_NOTIFICATIONS_SETUP.mdfor troubleshooting