This example demonstrates how to integrate Web Push notifications into a Next.js application by utilizing a custom service worker.
Workflow Summary:
- Generate VAPID Keys: Use the provided script to generate the necessary VAPID keys for secure push communication.
- Configure Environment: Store the generated keys in a
.env file so the application can access them. - Subscription Management: In a production environment, you should send the user's subscription data to your backend server to enable server-initiated notifications.
Note on Service Workers: When using custom workers for Web Push, ensure your .gitignore is configured to prevent build artifacts from being committed.
# 1. Bootstrap the example
npx create-next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/web-push web-push-app
# 2. Generate VAPID keys
pnpm vapid
# 3. Build and start
pnpm build
pnpm start