apns

package
v0.0.0-...-5012966 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2014 License: MIT Imports: 16 Imported by: 2

Documentation

Overview

Package apns provides an easy to use interface to communicate with the Apple Push Notification service (APNs)

Index

Constants

View Source
const (
	AppleResponseCommand = 8
	LocalResponseCommand = 0xDD // A command used strictly locally by the library
)
View Source
const (
	NoErrorsStatus                = 0
	ProcessingErrorStatus         = 1
	MissingDeviceTokenErrorStatus = 2
	MissingTopicErrorStatus       = 3
	MissingPayloadErrorStatus     = 4
	InvalidTokenSizeErrorStatus   = 5
	InvalidTopicSizeErrorStatus   = 6
	InvalidPayloadSizeErrorStatus = 7
	InvalidTokenErrorStatus       = 8
	ShutdownErrorStatus           = 10
	UnknownErrorStatus            = 255
)

Status that APNs can reply (byte 1)

View Source
const (
	RetryPushNotificationStatus    = 1
	CanceledPushNotificationStatus = 2
)
View Source
const FeedbackTimeoutSeconds = 5

Wait at most this many seconds for feedback data from Apple.

View Source
const MaxPayloadSizeBytes = 256

Your total notification payload cannot exceed 256 bytes.

View Source
const TimeoutSeconds = 5

The maximum number of seconds we're willing to wait for a response from the Apple Push Notification Service.

Variables

View Source
var ApplePushResponseDescriptions = map[uint8]string{
	NoErrorsStatus:                "NO_ERRORS",
	ProcessingErrorStatus:         "PROCESSING_ERROR",
	MissingDeviceTokenErrorStatus: "MISSING_DEVICE_TOKEN",
	MissingTopicErrorStatus:       "MISSING_TOPIC",
	MissingPayloadErrorStatus:     "MISSING_PAYLOAD",
	InvalidTokenSizeErrorStatus:   "INVALID_TOKEN_SIZE",
	InvalidTopicSizeErrorStatus:   "INVALID_TOPIC_SIZE",
	InvalidPayloadSizeErrorStatus: "INVALID_PAYLOAD_SIZE",
	InvalidTokenErrorStatus:       "INVALID_TOKEN",
	ShutdownErrorStatus:           "SHUTDOWN",
	UnknownErrorStatus:            "UNKNOWN",
}

This enumerates the response codes that Apple defines for push notification attempts.

View Source
var FeedbackChannel = make(chan (*FeedbackResponse))

FeedbackChannel will receive individual responses from Apple.

View Source
var LocalResponseDescriptions = map[uint8]string{
	RetryPushNotificationStatus:    "LOCAL_ERROR_RETRY",
	CanceledPushNotificationStatus: "LOCAL_ERROR_CANCEL",
}
View Source
var ShutdownChannel = make(chan bool)

If there's nothing to read, ShutdownChannel gets a true.

Functions

func NewQueue

func NewQueue() pushNotificationQueue

func StartMockFeedbackServer

func StartMockFeedbackServer(certFile, keyFile string)

StartMockFeedbackServer spins up a simple stand-in for the Apple feedback service that can be used for testing purposes. Doesn't handle many errors, etc. Just for the sake of having something "live" to hit.

Types

type AlertDictionary

type AlertDictionary struct {
	Body         string   `json:"body,omitempty"`
	ActionLocKey string   `json:"action-loc-key,omitempty"`
	LocKey       string   `json:"loc-key,omitempty"`
	LocArgs      []string `json:"loc-args,omitempty"`
	LaunchImage  string   `json:"launch-image,omitempty"`
}

AlertDictionary is a more complex notification payload.

From the APN docs: "Use the ... alert dictionary in general only if you absolutely need to." The AlertDictionary is suitable for specific localization needs.

func NewAlertDictionary

func NewAlertDictionary() *AlertDictionary

NewAlertDictionary creates and returns an AlertDictionary structure.

type ApplePushResponseStatus

type ApplePushResponseStatus uint8

ApplePushResponseStatus is the status type (byte 1)

type FeedbackResponse

type FeedbackResponse struct {
	Timestamp   uint32
	DeviceToken string
}

FeedbackResponse represents a device token that Apple has indicated should not be sent to in the future.

func NewFeedbackResponse

func NewFeedbackResponse() (resp *FeedbackResponse)

NewFeedbackResponse creates and returns a FeedbackResponse structure.

type Gateway

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

Gateway keeps all the informations needed to communicate with the APNs

func NewCustomGateway

func NewCustomGateway(ctx context.Context, gateway, certificateFile, keyFile string) (*Gateway, error)

NewCustomGateway create a client interface to a custom APNs server `gateway` format must be "hostname:port"

func NewGateway

func NewGateway(ctx context.Context, certificateFile, keyFile string) (*Gateway, error)

NewGateway creates a new gateway interface to the Apple APNs production servers

func NewSandboxGateway

func NewSandboxGateway(ctx context.Context, certificateFile, keyFile string) (*Gateway, error)

NewSandboxGateway creates a new gateway interface to the Apple APNs sandbox servers

func (*Gateway) Errors

func (g *Gateway) Errors(onError OnErrorCallback)

Errors gives feedback to the library client on which push notifications got errors The library client has to provide a callback via this method to get error informations.

func (*Gateway) Send

func (g *Gateway) Send(pn *PushNotification)

Send uses one of the gateway sender to send the push notification

type OnErrorCallback

type OnErrorCallback func(*PushNotification, *PushNotificationResponse)

OnErrorCallback functions are called to let the library client react when an error occured

type Payload

type Payload struct {
	Alert interface{} `json:"alert,omitempty"`
	Badge int         `json:"badge,omitempty"`
	Sound string      `json:"sound,omitempty"`
}

Payload contains the notification data for your request.

Alert is an interface here because it supports either a string or a dictionary, represented within by an AlertDictionary struct.

func NewPayload

func NewPayload() *Payload

NewPayload creates and returns a Payload structure.

type PersistentClient

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

PersistentClient opens a persistent connexion with the gateway

func NewPersistentClient

func NewPersistentClient(gateway, ip, certificateFile, keyFile string) (*PersistentClient, error)

NewPersistentClient creates a new persistent connection to the APNs servers

func (*PersistentClient) Close

func (c *PersistentClient) Close()

Close closes the persistent client

func (*PersistentClient) Connect

func (c *PersistentClient) Connect() error

Connect connects the persistent client to one of the APNs server If the connection is already established and was not closed, it does nothing.

func (*PersistentClient) Read

func (c *PersistentClient) Read(b []byte) (n int, err error)

func (*PersistentClient) Reconnect

func (c *PersistentClient) Reconnect() error

Reconnect forces a new connection to the gateway If a connection exists it is closed before the creation of a new one

func (*PersistentClient) Send

Send sends push notification to the APNs.

func (*PersistentClient) Write

func (c *PersistentClient) Write(b []byte) (n int, err error)

type PushNotification

type PushNotification struct {
	Identifier  uint32
	Expiry      uint32
	DeviceToken string

	Priority uint8
	// contains filtered or unexported fields
}

PushNotification is the wrapper for the Payload. The length fields are computed in ToBytes() and aren't represented here.

func NewPushNotification

func NewPushNotification() (pn *PushNotification)

NewPushNotification creates and returns a PushNotification structure. It also initializes the pseudo-random identifier.

func (*PushNotification) AddPayload

func (pn *PushNotification) AddPayload(p *Payload)

AddPayload sets the "aps" payload section of the request. It also has a hack described within to deal with specific zero values.

func (*PushNotification) Get

func (pn *PushNotification) Get(key string) interface{}

Get returns the value of a payload key, if it exists.

func (*PushNotification) PayloadJSON

func (pn *PushNotification) PayloadJSON() ([]byte, error)

PayloadJSON returns the current payload in JSON format.

func (*PushNotification) PayloadString

func (pn *PushNotification) PayloadString() (string, error)

PayloadString returns the current payload in string format.

func (*PushNotification) Set

func (pn *PushNotification) Set(key string, value interface{})

Set defines the value of a payload key.

func (*PushNotification) ToBytes

func (pn *PushNotification) ToBytes() ([]byte, error)

ToBytes returns a byte array of the complete PushNotification struct. This array is what should be transmitted to the APN Service.

type PushNotificationResponse

type PushNotificationResponse struct {
	Identifier      uint32
	Success         bool
	ResponseCommand PushResponseCommand
	ResponseStatus  ApplePushResponseStatus
	AppleResponse   string // Legacy field
	Error           error  // Legacy field
}

PushNotificationResponse details what Apple had to say, if anything.

func NewPushNotificationResponse

func NewPushNotificationResponse(pn *PushNotification) *PushNotificationResponse

NewPushNotificationResponse creates and returns a new PushNotificationResponse structure; it defaults to being unsuccessful at first.

func (*PushNotificationResponse) FromRawAppleResponse

func (pnr *PushNotificationResponse) FromRawAppleResponse(r []byte)

type PushResponseCommand

type PushResponseCommand uint8

Response command Apple response command is `AppleResponseCommand`

type Sender

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

Sender is informations needed to send push notifications to the APNs

func NewSender

func NewSender(ctx context.Context, gateway, ip, certificateFile, keyFile string) (*Sender, error)

NewSender creates a sender. It Manages the connection with the APNs, the waitingQueue of push notifications: - requeue the push notifications with a temporary error - report errors to the caller

func (*Sender) Send

func (s *Sender) Send(pn *PushNotification)

Send enqueues a new push notification to the sender and initiate the sending of all the push notification already queued

Jump to

Keyboard shortcuts

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