Skip to content

EmpireJones/webpush-go

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:      "mailto:<EMAIL@EXAMPLE.COM>",
		TTL:             60,
		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!
}

Dependencies

  1. Install Go 1.8+ (gvm recommended)
  2. Get gvt go get -u github.com/FiloSottile/gvt
  3. gvt restore

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

About

Web Push API Encryption with VAPID support.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%