/webpush-go

Web Push API Encryption with VAPID support.

Primary LanguageGoMIT LicenseMIT

webpush-go

Go Report Card GoDoc

Web Push API Encryption with VAPID support.

go get -u github.com/SherClockHolmes/webpush-go

Example

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:      "<EMAIL@EXAMPLE.COM>",
		VAPIDPrivateKey: vapidPrivateKey,
	})
	if err != nil {
		log.Fatal(err)
	}
}

Generating VAPID Keys

Use the helper method GenerateVAPIDKeys to generate the VAPID key pair.

privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
if err != nil {
    // TODO: Handle failure!
}

Development

  1. Install Go 1.8+ (gvm recommended)
  2. go get -u github.com/golang/dep/cmd/dep
  3. dep ensure

References

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

Similar Projects / Inspired By