hilla-push-notifications
hilla-push-notifications copied to clipboard
Example of sending Web Push notifications from Java
This is a full-stack example showing:
- How to subscribe (and unsubscribe) to notifications in the browser
- How to send push notifications from a Java (Spring Boot) server
Text tutorial: https://dev.to/marcushellberg/how-to-send-web-push-notifications-in-java-21lo Video tutorial: https://youtu.be/M5sbGvW3S4I
Tech stack
The project uses Hilla. It has a Spring Boot backend (Java) and a Lit frontend (TypeScript). It uses the Java WebPush library for sending messages.
Pre-requisites
- Java 17+
- Maven 3.6+
- Node 16.14+
Running the app
- First, generate required a private and public key pair for your server with
npx web-push generate-vapid-keys. - Rename
.env.templateto.envand add your keys to it. - Load the environment file with
source .env - Start the Spring Boot application with
mvnor by runningApplication.java.
Navigating the code
frontend/views/notifications/notifications-view.tscontains the client implementation for subscribing and unsubscribingsrc/main/java/com/example/application/MessageService.javacontains the code for sending push messagessrc/main/java/com/example/application/MessageEndpoint.javacontains the code for accessing the server