Self implementing Web Push into a Svelte Kit App

hartenfellerdev

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 Push Notifications

0:00 - 1 min, 1 sec

Introduction to the concept and utility of push notifications for web applications.

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 the escalet Demo App

1:01 - 1 min, 18 sec

Setting up and explaining the escalet demo app used for the push notification example.

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

Requesting Notification Permissions

2:19 - 1 min, 4 sec

Handling user permissions for receiving notifications.

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

Implementing Push Subscriptions

3:22 - 2 min, 37 sec

Implementation of push notifications includes generating keys and creating subscriptions.

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

Setting Up Backend for Notifications

6:00 - 2 min, 17 sec

Backend setup includes adding new dependencies and generating VAPID keys.

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

Service Worker and Subscription Flow

8:17 - 1 min, 43 sec

Explaining the service worker's role and the subscription flow in push notifications.

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

Front End Notification Logic

10:00 - 4 min, 10 sec

Building the front end logic to handle notification subscription and unsubscription.

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 Notifications

14:10 - 15 min, 41 sec

Testing the notification system with the service worker and handling failed notifications.

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

Sending Notifications and Wrapping Up

29:51 - 3 min, 5 sec

Final steps of implementing and testing notifications, including adding users to notification channels.

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.