Categorygithub.com/neurohotep/webpush-go
modulepackage
0.0.1
Repository: https://github.com/neurohotep/webpush-go.git
Documentation: pkg.go.dev

# README

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 protected]>",
		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

# Functions

GenerateVAPIDKeys will create a private and public VAPID key pair.
SendNotification sends a push notification to a subscriptions endpoint Follows the Message Encryption for Web Push, and VAPID protocols.

# Constants

UrgencyHigh admits device state: low battery.
UrgencyLow requires device state: on either power or Wi-Fi.
UrgencyNormal excludes device state: low battery.
UrgencyVeryLow requires device state: on power and Wi-Fi.

# Structs

Keys are the base64 encoded values from PushSubscription.getKey().
Options are config and extra params needed to send a notification.
Subscription represents a PushSubscription object from the Push API.

# Interfaces

HTTPClient is an exposed interface to pass in custom http.Client.

# Type aliases

Urgency indicates to the push service how important a message is to the user.