apns

package
v0.0.0-...-e390b1b Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2014 License: MIT Imports: 19 Imported by: 0

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 NewPNQueue

func NewPNQueue() 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 map[string]interface{}

Payload contains the notification data for your request.

func NewPayload

func NewPayload() Payload

NewPayload creates a new Payload

func (Payload) SetAlertDictionary

func (p Payload) SetAlertDictionary(alert AlertDictionary)

func (Payload) SetAlertString

func (p Payload) SetAlertString(alert string)

func (Payload) SetBadge

func (p Payload) SetBadge(badge int)

func (Payload) SetSound

func (p Payload) SetSound(sound string)

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) 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) SetPayload

func (pn *PushNotification) SetPayload(p Payload)

AddPayload sets the "aps" payload section of the request.

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 PushNotificationRequestResponse

type PushNotificationRequestResponse struct {
	Notification *PushNotification
	Response     *PushNotificationResponse
}

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 sends notifications

func NewSender

func NewSender(ctx context.Context, addr string, cert *tls.Certificate) *Sender

NewSender creates a new Sender

func (*Sender) Notifications

func (s *Sender) Notifications() chan *PushNotification

Notifications returns the channel to which to send notifications

func (*Sender) Responses

func (s *Sender) Responses() <-chan *PushNotificationRequestResponse

Responses returns the channel from which responses should be received

Jump to

Keyboard shortcuts

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