This example demonstrates how to use the webpush-go library to send push notifications.
-
Start the server:
go run main.go
The server will start on port 8080 and generate the VAPID keys.
-
Subscribe to notifications:
Open your browser and navigate to
http://localhost:8080. The browser will ask for permission to show notifications. Click "Allow". -
Send a test notification:
Click the "Send Test Notification" button. You should receive a push notification in your browser.
- The
main.gofile starts a web server that serves theindex.htmlandservice-worker.jsfiles. - When you visit
index.html, the JavaScript code subscribes to push notifications and sends the subscription object to the server. - The server stores the subscription in memory.
- When you click the "Send Test Notification" button, the browser sends a request to the server, which then uses the
webpush-golibrary to send a notification to your browser.
The example has crude browser detection and sends the appropriate VAPID authentication scheme to the server.
- For Chrome, it uses the
webpushscheme. - For other browsers, it uses the
vapidscheme.
This is handled in the index.html file, where the browser is detected and the authScheme is sent to the server when a notification is requested. The main.go server then uses this authScheme to send the notification with the correct VAPID headers.