Generate VAPID Public and Private Keys for the notification service. This can be done using many tools, one of which is GenerateVAPIDKeys
from webpush-go.
This service is compatible with the Web Push Protocol and VAPID.
For a list of compatible browsers, see this for the Push API and this for the Web Notifications.
package main
import (
"context"
"log"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/webpush"
)
const vapidPublicKey = "..." // Add a vapidPublicKey
const vapidPrivateKey = "..." // Add a vapidPrivateKey
func main() {
subscription := webpush.Subscription{
Endpoint: "https://your-endpoint",
Keys: {
Auth: "...",
P256dh: "...",
},
}
webpushSvc := webpush.New(vapidPublicKey, vapidPrivateKey)
webpushSvc.AddReceivers(subscription)
notifier := notify.NewWithServices(webpushSvc)
if err := notifier.Send(context.Background(), "TEST", "Message using golang notifier library"); err != nil {
log.Fatalf("notifier.Send() failed: %s", err.Error())
}
log.Println("Notification sent successfully")
}