webpush

package module
v0.0.0-...-2561e00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2017 License: MIT Imports: 21 Imported by: 0

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:      "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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateVAPIDKeys

func GenerateVAPIDKeys() (privateKey, publicKey string, err error)

GenerateVAPIDKeys will create a private and public VAPID key pair

func SendNotification

func SendNotification(message []byte, s *Subscription, options *Options) (*http.Response, error)

SendNotification sends a push notification to a subscriptions endpoint Follows the Message Encryption for Web Push, and VAPID protocols

Types

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

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

type Keys

type Keys struct {
	Auth   string `json:"auth"`
	P256dh string `json:"p256dh"`
}

Keys are the base64 encoded values from PushSubscription.getKey()

type Options

type Options struct {
	HTTPClient      HTTPClient // Will replace with *http.Client by default if not included
	Subscriber      string     // Sub in VAPID JWT token
	TTL             int        // Set the TTL on the endpoint POST request
	VAPIDPrivateKey string     // Used to sign VAPID JWT token
}

Options are config and extra params needed to send a notification

type Subscription

type Subscription struct {
	Endpoint string `json:"endpoint"`
	Keys     Keys   `json:"keys"`
}

Subscription represents a PushSubscription object from the Push API

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL