push

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2019 License: MIT Imports: 13 Imported by: 49

Documentation

Overview

Package push sends notifications over HTTP/2 to Apple's Push Notification Service.

Index

Constants

View Source
const (
	Development     = "https://api.development.push.apple.com"
	Development2197 = "https://api.development.push.apple.com:2197"
	Production      = "https://api.push.apple.com"
	Production2197  = "https://api.push.apple.com:2197"
)

Apple host locations for configuring Service.

Variables

View Source
var (
	// These could be checked prior to sending the request to Apple.
	ErrPayloadEmpty    = errors.New("PayloadEmpty")
	ErrPayloadTooLarge = errors.New("PayloadTooLarge")

	// Device token errors.
	ErrMissingDeviceToken = errors.New("MissingDeviceToken")
	ErrBadDeviceToken     = errors.New("BadDeviceToken")
	ErrTooManyRequests    = errors.New("TooManyRequests")

	// Header errors.
	ErrBadMessageID      = errors.New("BadMessageID")
	ErrBadExpirationDate = errors.New("BadExpirationDate")
	ErrBadPriority       = errors.New("BadPriority")
	ErrBadTopic          = errors.New("BadTopic")
	ErrInvalidPushType   = errors.New("InvalidPushType")

	// Certificate and topic errors.
	ErrBadCertificate            = errors.New("BadCertificate")
	ErrBadCertificateEnvironment = errors.New("BadCertificateEnvironment")
	ErrForbidden                 = errors.New("Forbidden")
	ErrMissingTopic              = errors.New("MissingTopic")
	ErrTopicDisallowed           = errors.New("TopicDisallowed")
	ErrUnregistered              = errors.New("Unregistered")
	ErrDeviceTokenNotForTopic    = errors.New("DeviceTokenNotForTopic")

	// These errors should never happen when using Push.
	ErrDuplicateHeaders = errors.New("DuplicateHeaders")
	ErrBadPath          = errors.New("BadPath")
	ErrMethodNotAllowed = errors.New("MethodNotAllowed")

	// Fatal server errors.
	ErrIdleTimeout         = errors.New("IdleTimeout")
	ErrShutdown            = errors.New("Shutdown")
	ErrInternalServerError = errors.New("InternalServerError")
	ErrServiceUnavailable  = errors.New("ServiceUnavailable")
)

Service error responses.

Functions

func IsDeviceTokenValid added in v0.3.0

func IsDeviceTokenValid(s string) bool

IsDeviceTokenValid checks if s is a hexadecimal token of the correct length.

func NewClient

func NewClient(cert tls.Certificate) (*http.Client, error)

NewClient sets up an HTTP/2 client for a certificate.

Types

type Error added in v0.3.0

type Error struct {
	Reason    error
	Status    int // http StatusCode
	Timestamp time.Time
}

Error responses from Apple

func (*Error) Error added in v0.3.0

func (e *Error) Error() string

type Headers

type Headers struct {
	// ID for the notification. Apple generates one if omitted.
	// This should be a UUID with 32 lowercase hexadecimal digits.
	ID string

	// CollapseID is used to update an existing notification that has the same
	// identifier (Notification Management in iOS 10).
	CollapseID string

	// Apple will retry delivery until this time. The default behavior only tries once.
	Expiration time.Time

	// Allow Apple to group messages together to reduce power consumption.
	// By default messages are sent immediately.
	LowPriority bool

	// Topic for certificates with multiple topics.
	Topic string

	// Type of push
	Type Type
}

Headers sent with a push to control the notification (optional)

type Queue added in v0.7.0

type Queue struct {
	Responses chan Response
	// contains filtered or unexported fields
}

Queue up notifications without waiting for the response.

func NewQueue added in v0.7.0

func NewQueue(service *Service, workers uint) *Queue

NewQueue wraps a service with a queue for sending notifications asynchronously.

func (*Queue) Close added in v0.12.0

func (q *Queue) Close()

Close the channels for notifications and Responses and shutdown workers. You should only call this after all responses have been received.

func (*Queue) Push added in v0.7.0

func (q *Queue) Push(deviceToken string, headers *Headers, payload []byte)

Push queues a notification to the APN service.

type Response added in v0.8.0

type Response struct {
	DeviceToken string
	ID          string
	Err         error
}

Response from sending a notification.

type Service

type Service struct {
	Host   string
	Client *http.Client
}

Service is the Apple Push Notification Service that you send notifications to.

func NewService added in v0.5.0

func NewService(client *http.Client, host string) *Service

NewService creates a new service to connect to APN.

func (*Service) Push

func (s *Service) Push(deviceToken string, headers *Headers, payload []byte) (string, error)

Push sends a notification and waits for a response.

type Type added in v0.14.0

type Type string

Type of push

const (
	Alert        Type = "alert"
	Background   Type = "background"
	VoIP         Type = "voip"
	Complication Type = "complication"
	FileProvider Type = "fileprovider"
	MDM          Type = "mdm"
)

Available push types

Jump to

Keyboard shortcuts

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