webpush

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2019 License: MIT Imports: 21 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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() `

	// Decode subscription
	s := webpush.Subscription
	if err := json.NewDecoder(bytes.NewBufferString(subJSON)).Decode(&s); err != nil 

	// Send Notification
	_, err := webpush.SendNotification([]byte("Test"), &s, &webpush.Options)
	if err != nil 

Generating VAPID Keys

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

privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
if err != nil 

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

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
	Topic           string     // Set the Topic header to collapse a pending messages (Optional)
	TTL             int        // Set the TTL on the endpoint POST request
	Urgency         Urgency    // Set the Urgency header to change a message priority (Optional)
	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

type Urgency

type Urgency string

Urgency indicates to the push service how important a message is to the user. This can be used by the push service to help conserve the battery life of a user's device by only waking up for important messages when battery is low.

const (
	// UrgencyVeryLow requires device state: on power and Wi-Fi
	UrgencyVeryLow Urgency = "very-low"
	// UrgencyLow requires device state: on either power or Wi-Fi
	UrgencyLow Urgency = "low"
	// UrgencyNormal excludes device state: low battery
	UrgencyNormal Urgency = "normal"
	// UrgencyHigh admits device state: low battery
	UrgencyHigh Urgency = "high"
)

Jump to

Keyboard shortcuts

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