Skip to content

Web Push API Encryption with VAPID support.

License

Notifications You must be signed in to change notification settings

SherClockHolmes/webpush-go

Folders and files

NameName
Last commit message
Last commit date
Apr 30, 2021
Oct 3, 2023
Feb 18, 2019
Dec 24, 2016
Oct 27, 2019
Jan 2, 2025
Jan 2, 2025
Feb 18, 2019
Jan 2, 2025
Jan 2, 2025
Jan 2, 2025
May 28, 2020

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

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
	resp, 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
	}
	defer resp.Body.Close()
}

Generating VAPID Keys

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

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

Development

  1. Install Go 1.11+
  2. go mod vendor
  3. go test

For other language implementations visit:

WebPush Libs