Web Push API Encryption with VAPID support.
go get -u github.com/SherClockHolmes/webpush-go
package main
import (
"bytes"
"encoding/json"
"log"
webpush "github.com/sherclockholmes/webpush-go"
)
const (
vapidPrivateKey = "<YOUR VAPID PRIVATE KEY>"
)
func main() {
subJSON := `{<YOUR SUBSCRIPTION JSON>}`
// Decode subscription
s := webpush.Subscription{}
if err := json.NewDecoder(bytes.NewBufferString(subJSON)).Decode(&s); err != nil {
log.Fatal(err)
}
// Send Notification
_, err := webpush.SendNotification([]byte("Test"), &s, &webpush.Options{
Subscriber: "mailto:<[email protected]>",
TTL: 60,
VAPIDPrivateKey: vapidPrivateKey,
})
if err != nil {
log.Fatal(err)
}
}
Use the helper method GenerateVAPIDKeys
to generate the VAPID key pair.
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
if err != nil {
// TODO: Handle failure!
}
For more information visit these Google Developers links:
https://developers.google.com/web/updates/2016/03/web-push-encryption
https://developers.google.com/web/updates/2016/07/web-push-interop-wins