Web Push API Encryption with VAPID support.
go get -u github.com/SherClockHolmes/webpush-go
For a full example, refer to the code in the example directory.
package main
import (
"encoding/json"
webpush "github.com/SherClockHolmes/webpush-go"
)
func main() {
// Decode subscription
s := &webpush.Subscription{}
json.Unmarshal([]byte("<YOUR_SUBSCRIPTION>"), s)
// Send Notification
_, err := webpush.SendNotification([]byte("Test"), s, &webpush.Options{
Subscriber: "[email protected]",
VAPIDPublicKey: "<YOUR_VAPID_PUBLIC_KEY>",
VAPIDPrivateKey: "<YOUR_VAPID_PRIVATE_KEY>",
TTL: 30,
})
if err != nil {
// TODO: Handle error
}
}
Use the helper method GenerateVAPIDKeys
to generate the VAPID key pair.
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
if err != nil {
// TODO: Handle error
}
- Install Go 1.11+
go mod vendor
go test