apns

package
v0.0.0-...-7df2def Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2015 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package apns exposes APNS Provider for Apple's APNS and Feedback services and uses job/worker pattern to process notifications concurrently. Each worker establishes it's own TLS connection to APNS gateway. When an error response is received from APNS server, worker tries to reconnect automatically.

Index

Constants

View Source
const (
	// TimestampItemLength is the length of timestamp item
	TimestampItemLength = 4
	// DeviceTokenLengthItemLength is the length of length of device token item
	DeviceTokenLengthItemLength = 2
)
View Source
const (
	// DeviceTokenItemID is the ID of device token item in apns binary protocol
	DeviceTokenItemID = 1
	// DeviceTokenItemLength is the length of the device token item
	DeviceTokenItemLength = 32
	// PayloadItemID is the ID of payload item in apns binary protocol
	PayloadItemID = 2
	// PayloadItemMaxLength is the maximum length of the payload item
	PayloadItemMaxLength = 2048
	// NotificationIdentifierItemID is the ID of notification identifier item in apns binary protocol
	NotificationIdentifierItemID = 3
	// NotificationIdentifierItemLength is the length of notification identifier item
	NotificationIdentifierItemLength = 4
	// ExpirationDateItemID is the ID of expiration date item in apns binary protocol
	ExpirationDateItemID = 4
	// ExpirationDateItemLength is the length of expiration date item
	ExpirationDateItemLength = 4
	// PriorityItemID is the ID of priority item in apns binary protocol
	PriorityItemID = 5
	// PriorityItemLength is the length of priority item
	PriorityItemLength = 1
)
View Source
const (
	// APNSGatewayProduction ...
	APNSGatewayProduction = "gateway.push.apple.com"

	// APNSGatewaySandbox ...
	APNSGatewaySandbox = "gateway.sandbox.push.apple.com"

	// APNSGatewayPort ...
	APNSGatewayPort uint16 = 2195

	// FeedbackGatewayProduction ...
	FeedbackGatewayProduction = "feedback.push.apple.com"

	// FeedbackGatewaySandbox ...
	FeedbackGatewaySandbox = "feedback.sandbox.push.apple.com"

	// FeedbackGatewayPort ...
	FeedbackGatewayPort uint16 = 2196
)
View Source
const (
	// CommandsQueueSize specifies default notifications queue size
	CommandsQueueSize = 100000
)
View Source
const SendNotificationCommandValue = 2

SendNotificationCommandValue is the value of send push notification command in apns binary protocol

Variables

View Source
var PushNotificationErrorStatuses = map[uint8]string{
	0:   "No errors encountered",
	1:   "Processing error",
	2:   "Missing device token",
	3:   "Missing topic",
	4:   "Missing payload",
	5:   "Invalid token size",
	6:   "Invalid topic size",
	7:   "Invalid payload size",
	8:   "Invalid token",
	10:  "Shutdown",
	255: "Unknown",
}

PushNotificationErrorStatuses represents APNS error status codes (https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW12)

Functions

func SetLogger

func SetLogger(l LoggerInterface)

SetLogger sets the package logger

func SetupCommandLineFlags

func SetupCommandLineFlags(fs *pflag.FlagSet)

SetupCommandLineFlags sets all necessary command line flags and their defaults

Types

type Alert

type Alert struct {
	Title                  string   `json:"title,omitempty", mapstructure:"title"`
	Body                   string   `json:"body,omitempty", mapstructure:"body"`
	TitleLocalizationKey   string   `json:"title-loc-key,omitempty", mapstructure:"title-loc-key"`
	TitleLocalizationdArgs []string `json:"title-loc-args,omitempty", mapstructure:"title-loc-args"`
	ActionLocalizationKey  string   `json:"action-loc-key,omitempty", mapstructure:"action-loc-key"`
	BodyLocalizationKey    string   `json:"loc-key,omitempty", mapstructure:"loc-key"`
	BodyLocalizationArgs   []string `json:"loc-args,omitempty", mapstructure:"loc-args"`
	LaunchImage            string   `json:"launch-image,omitempty", mapstructure:"launch-image"`
}

Alert struct represents alert dictionary (https://developer.apple.com/library/prerelease/watchos/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW20)

type Aps

type Aps struct {
	Alert            interface{} `json:"alert,omitempty"`
	Badge            int         `json:"badge,omitempty"`
	Sound            string      `json:"sound,omitempty"`
	ContentAvailable int         `json:"content-available,omitempty"`
	Category         string      `json:"category,omitempty"`
}

Aps struct represents aps dictionary (https://developer.apple.com/library/prerelease/watchos/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW2)

func NewAps

func NewAps() *Aps

NewAps creates a new blank notification payload aps object

type Client

type Client struct {
	Config *ClientConfig
	// contains filtered or unexported fields
}

Client struct is the main class for interacting with Apple Push Notification Service

func NewClient

func NewClient(config *ClientConfig) (client *Client, err error)

NewClient creates a new Client

func (*Client) CheckFeedbackService

func (c *Client) CheckFeedbackService() (rsp *FeedbackResponse, err error)

CheckFeedbackService connects to Apple's feedback service and returns FeedbackResponse object

func (*Client) Errors

func (c *Client) Errors() <-chan CommandErrorInterface

Errors returns a channel to consume command errors

func (*Client) ExecuteCommand

func (c *Client) ExecuteCommand(cmd CommandInterface) error

ExecuteCommand queues command for execution

type ClientConfig

type ClientConfig struct {
	// Env is either "production" or "sandbox"
	Env string

	// NumberOfWorkers sets number of workers for sending push notifications
	NumberOfWorkers uint32

	// CertificateFile is absolute path to APNS certificate file
	CertificateFile string

	// CertificatePrivateKey is absolute path to APNS certificate private key file
	CertificatePrivateKeyFile string

	// CommandsQueueSize sets the queue size for push notifications
	CommandsQueueSize uint64
}

ClientConfig holds some configuration options for Client

func NewClientConfig

func NewClientConfig() (config *ClientConfig)

NewClientConfig returns new client config

type CommandError

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

CommandError is a generic command error

func NewCommandError

func NewCommandError(err error, cmd CommandInterface) *CommandError

NewCommandError creates and returns new generic command execution error

func NewCommandErrorFromAPNSResponse

func NewCommandErrorFromAPNSResponse(data []byte, cmd CommandInterface) (commandError *CommandError)

NewCommandErrorFromAPNSResponse creates and returns error representing APNS response

func (*CommandError) Error

func (ge *CommandError) Error() string

Error implements standard go error interface

func (*CommandError) GetCommand

func (ge *CommandError) GetCommand() CommandInterface

GetCommand returns command this error belongs to

func (*CommandError) GetError

func (ge *CommandError) GetError() error

GetError returns underlying error object

type CommandErrorInterface

type CommandErrorInterface interface {
	Error() string
	GetError() error
	GetCommand() CommandInterface
}

CommandErrorInterface specifies and interface for command execution errors

type CommandInterface

type CommandInterface interface {
	Identifier() string
	Bytes() ([]byte, error)
	Data() interface{}
	String() string
	Errors() chan CommandErrorInterface
}

CommandInterface specifies an interface for APNS commands

type FeedbackDeviceEntry

type FeedbackDeviceEntry struct {
	Timestamp   time.Time `json:"timestamp"`
	DeviceToken string    `json:"deviceToken"`
}

FeedbackDeviceEntry struct represents feedback tuple (https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW5)

func NewFeedbackDeviceEntry

func NewFeedbackDeviceEntry() *FeedbackDeviceEntry

NewFeedbackDeviceEntry returns a new feedback tuple object

type FeedbackResponse

type FeedbackResponse struct {
	Devices []*FeedbackDeviceEntry `json:"devices"`
}

FeedbackResponse holds all device entries from feedback service response

func NewFeedbackResponse

func NewFeedbackResponse() *FeedbackResponse

NewFeedbackResponse returns a new feedback tuple object

type LoggerInterface

type LoggerInterface interface {
	Println(args ...interface{})
	Printf(format string, args ...interface{})
	Print(args ...interface{})

	Panicf(format string, args ...interface{})
	Panic(args ...interface{})

	Fatalf(format string, args ...interface{})
	Fatal(args ...interface{})

	Errorf(format string, args ...interface{})
	Error(entries ...interface{})

	Warningf(format string, args ...interface{})
	Warning(entries ...interface{})

	Noticef(format string, args ...interface{})
	Notice(entries ...interface{})

	Infof(format string, args ...interface{})
	Info(entries ...interface{})

	Debugf(format string, args ...interface{})
	Debug(entries ...interface{})
}

LoggerInterface specifies type of Logger that the library requires

type Notification

type Notification struct {
	DeviceToken            string     `json:"deviceToken,omitempty"`
	Payload                *Payload   `json:"payload,omitempty"`
	NotificationIdentifier string     `json:"identifier,omitempty"`
	ExpirationDate         *time.Time `json:"expires,omitempty"`
	Priority               uint8      `json:"priority,omitempty"`
}

Notification struct represents push notification

func NewNotification

func NewNotification() *Notification

NewNotification creates a new blank notification object

func (*Notification) UnmarshalJSON

func (n *Notification) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements custom marshalling of notification json

type Payload

type Payload struct {
	Aps *Aps `json:"aps,omitempty"`
	// contains filtered or unexported fields
}

Payload struct represents the whole notification payload (https://developer.apple.com/library/prerelease/watchos/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1)

func NewPayload

func NewPayload() *Payload

NewPayload creates a new blank notification payload object

func (*Payload) AddCustomField

func (p *Payload) AddCustomField(key string, value interface{})

AddCustomField adds custom field to notification payload

func (*Payload) JSON

func (p *Payload) JSON() ([]byte, error)

JSON returns payload data marshalled into JSON

func (*Payload) JSONString

func (p *Payload) JSONString() (string, error)

JSONString returns payload data as JSON string

func (*Payload) MarshalJSON

func (p *Payload) MarshalJSON() (jsonBytes []byte, err error)

MarshalJSON implements custom marshalling of notification payload to json

type PushNotificationCommand

type PushNotificationCommand struct {
	Notification *Notification
	// contains filtered or unexported fields
}

PushNotificationCommand represents command for sending push notification

func NewPushNotificationCommand

func NewPushNotificationCommand(n *Notification) (cmd *PushNotificationCommand)

NewPushNotificationCommand creates a new send push notifiction command

func (*PushNotificationCommand) Bytes

func (cmd *PushNotificationCommand) Bytes() ([]byte, error)

Bytes returns send push notification command data

func (*PushNotificationCommand) Data

func (cmd *PushNotificationCommand) Data() interface{}

Data returns data associated with command, in this case the Notification struct

func (*PushNotificationCommand) Errors

Errors returns a channel to which errors will be sent

func (*PushNotificationCommand) Identifier

func (cmd *PushNotificationCommand) Identifier() string

Identifier returns command identifier (in this case notification identifier)

func (*PushNotificationCommand) String

func (cmd *PushNotificationCommand) String() string

String returns a human readable description of the command

Jump to

Keyboard shortcuts

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