webpush

package module
v0.0.0-...-835ec73 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: BSD-3-Clause Imports: 22 Imported by: 0

README

webpush

Package webpush implements the Web Push protocol.

Usage

c, err := webpush.New(priv, "mailto:foo@example.com")
if err != nil {
  log.Fatal(err)
}
s := &webpush.Subscription{
  Endpoint:   "https://example.com",
  PublicKey:  webpush.DecodeKey(p256dh),
  PrivateKey: webpush.DecodeKey(auth),
}
resp, err := c.Push(s, []byte("hello, world"), webpush.WithTTL(0))
if err != nil {
  log.Fatal(err)
}
defer resp.Body.Close()

Documentation

Overview

Package webpush implements Web Push helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeKey

func DecodeKey(s string) ([]byte, error)

DecodeKey returns the bytes represented by the base64 string s. This can be used to decode new subscription p256dh and auth keys.

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey returns a new ECDSA private key from a secure random source.

Types

type Option

type Option func(*WebPush)

Option represents a functional option for configuration.

func WithExpiry

func WithExpiry(d time.Duration) Option

WithExpiry sets the duration with which to expire the push.

func WithPusher

func WithPusher(pusher Pusher) Option

WithPusher sets the pusher. Defaults to a sane HTTP client for most use cases.

type PushOption

type PushOption func(req *http.Request)

PushOption represents a functional option for push configuration.

func WithTTL

func WithTTL(ttl int) PushOption

WithTTL sets the TTL header. Defaults to 60 seconds.

func WithTopic

func WithTopic(topic string) PushOption

WithTopic sets the Topic header.

func WithUrgency

func WithUrgency(urgency Urgency) PushOption

WithUrgency sets the Urgency header.

type Pusher

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

Pusher represents the ability to perform web push requests.

type Subscription

type Subscription struct {
	Endpoint   string
	PublicKey  []byte // p256dh
	PrivateKey []byte // auth
}

Subscription represents a subscription to a Web Push service.

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 the device to be on power and wifi.
	// Useful for advertisements.
	UrgencyVeryLow Urgency = "very-low"

	// UrgencyLow requires the device to be on either power or wifi.
	// Useful for topic updates.
	UrgencyLow Urgency = "low"

	// UrgencyNormal requires the device to be on neither power nor wifi.
	// Useful for chats or calendar reminders.
	UrgencyNormal Urgency = "normal"

	// UrgencyHigh will operate on low battery.
	// Useful for time-sensitive alerts.
	UrgencyHigh Urgency = "high"
)

func (Urgency) String

func (u Urgency) String() string

String implements the fmt.Stringer interface.

type WebPush

type WebPush struct {
	// contains filtered or unexported fields
}

WebPush represents the Web Push application server.

func New

func New(privateKey []byte, subscriber string, opts ...Option) (*WebPush, error)

New returns a new Web Push application server. privateKey must be a PEM-encoded ECDSA private key. Generate a valid key with GenerateKey.

func (*WebPush) PublicKey

func (c *WebPush) PublicKey() string

PublicKey returns the public application server key for use in JavaScript client web push subscription.

func (*WebPush) Push

func (c *WebPush) Push(s *Subscription, b []byte, opts ...PushOption) (*http.Response, error)

Push sends a push notification.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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