Self implementing Web Push into a Svelte Kit App
hartenfellerdev
32 min, 58 sec
A detailed guide on implementing push notifications for a web application without third-party services.
Summary
- The process involves sending notifications from a server to Apple or Google servers, which then push the notification to the appropriate device.
- A demonstration is provided using an example music store web application called 'escalet demo app'.
- The video covers setting up the backend for notifications, handling user permissions, creating a service worker, and testing notifications.
- The code for the implementation is available on GitHub and the process does not rely on third-party services, making it cost-effective.
Chapter 1

Introduction to the concept and utility of push notifications for web applications.
- The goal is to send notifications from a server directly to a user's device, which can be a phone or a computer.
- These notifications are useful for immediate alerts, such as when a new message arrives from a messaging app.
- The website does not need to be open; only the browser needs to be active for notifications to work.

Chapter 2

Setting up and explaining the escalet demo app used for the push notification example.
- The demo app is a music store application built for demonstration purposes, with its code available on GitHub.
- A settings page allows logged-in users to enable push notifications for new album updates.
- Users must give permission for the browser to send notifications to reduce spam.

Chapter 3

Handling user permissions for receiving notifications.
- A check is performed on page load to see if notification permissions have been granted by the user.
- If not already granted, the app displays a button that, when clicked, requests permission to send notifications.
- The user can manually allow or block notifications from the browser prompt.

Chapter 4

Implementation of push notifications includes generating keys and creating subscriptions.
- A private-public key pair called VAPID keys is generated for secure communication.
- The public key is shared with the front end, while the private key is kept secret in the backend environment.
- The app requests the public key from the backend and then subscribes the user through the service worker.

Chapter 5

Backend setup includes adding new dependencies and generating VAPID keys.
- New dependencies like 'web-push' are added to the backend to facilitate notification management.
- The VAPID keys are generated using a command-line utility and stored in the environment file for later use.
- An endpoint is created to provide the public key to the front end.

Chapter 6

Explaining the service worker's role and the subscription flow in push notifications.
- A service worker script is used to run background tasks like notifications.
- The app asks the service worker to create a push subscription using the public key.
- Subscription details are then sent back to the server to be stored for future notification pushes.

Chapter 7

Building the front end logic to handle notification subscription and unsubscription.
- The settings page is updated to include functionality to generate a subscription with the public key.
- Functions are added to check the subscription status and to allow users to unsubscribe.
- When a subscription is created or found, it is sent to the backend for storage.

Chapter 8

Testing the notification system with the service worker and handling failed notifications.
- The service worker is updated with an event listener for push events to display notifications.
- Testing the notification system ensures that it works as expected.
- A logic is added to delete subscriptions if multiple notification attempts fail, to avoid redundant data.

Chapter 9

Final steps of implementing and testing notifications, including adding users to notification channels.
- A test notification is implemented and a button is added to trigger it.
- Notifications are successfully sent and received across different browsers and devices.
- The tutorial concludes with a demonstration of the working notification system.
