apns

package module
v0.0.0-...-c1bf99e Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2016 License: MIT Imports: 14 Imported by: 0

README

apns

Client library for Apple Push Notification, focusing on reliability.

Example usage

package main

import (
	"crypto/tls"

	"golang.org/x/net/context"
	"github.com/mentionapp/apns.go"
)

func main() {

	cert, err := tls.LoadX509KeyPair("cert.crt", "cert.key")
	if err != nil {
		panic(err)
	}
	
	apns.Verbose = true

	sender := apns.NewSender(context.TODO(), apns.SenderSandboxGateway, &cert)

	go func() {
		// sender.Errors() is a channel
		// It receives permanent sending errors (e.g. the token is invalid).
		// For this kind of errors, the library doesn't retry. The application
		// might want to take action, though.
		for senderError := range sender.Errors() {
			switch senderError.ErrorResponse.Status {
			case apns.InvalidTokenSizeErrorStatus, apns.InvalidTokenErrorStatus:
				// We tried to send a message to an invalid token
			}
		}
	}()

	// msgs is an imaginary channel with messages to send
	for msg := range msgs {
		notif := apns.NewNotification()
		notif.SetDeviceToken(msg.Token)
		payload := &apns.Payload{}
		payload.SetAlertString(msg.Text)
		notif.SetPayload(payload)
		sender.Notifications() <- notif
	}
}

Credits

Documentation

Overview

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

Index

Constants

View Source
const (
	FeedbackGateway        string = "feedback.push.apple.com:2196"
	FeedbackSandboxGateway string = "feedback.sandbox.push.apple.com:2196"
)
View Source
const (
	SenderGateway        string = "gateway.push.apple.com:2195"
	SenderSandboxGateway string = "gateway.sandbox.push.apple.com:2195"
)
View Source
const MaxPayloadLen = 256

MaxPayloadLen is the maximum allowed payload length (after JSON encoding)

Variables

View Source
var Verbose bool

Functions

This section is empty.

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 localized alert text

func NewAlertDictionary

func NewAlertDictionary() *AlertDictionary

NewAlertDictionary creates a new AlertDictionary

type ErrorResponse

type ErrorResponse struct {
	Command    ErrorResponseCommand
	Status     ErrorResponseStatus
	Identifier NotificationIdentifier
}

ErrorResponse represents an APNS error-response packet

type ErrorResponseCommand

type ErrorResponseCommand uint8

ErrorResponseCommand represents the Command field of error-response packets

const (
	ErrorCommand ErrorResponseCommand = 8
)

Known values of ErrorResponseCommand

type ErrorResponseStatus

type ErrorResponseStatus uint8

ErrorResponseStatus represents the Status field of error-response packets

const (
	NoErrorsStatus                ErrorResponseStatus = 0
	ProcessingErrorStatus         ErrorResponseStatus = 1
	MissingDeviceTokenErrorStatus ErrorResponseStatus = 2
	MissingTopicErrorStatus       ErrorResponseStatus = 3
	MissingPayloadErrorStatus     ErrorResponseStatus = 4
	InvalidTokenSizeErrorStatus   ErrorResponseStatus = 5
	InvalidTopicSizeErrorStatus   ErrorResponseStatus = 6
	InvalidPayloadSizeErrorStatus ErrorResponseStatus = 7
	InvalidTokenErrorStatus       ErrorResponseStatus = 8
	ShutdownErrorStatus           ErrorResponseStatus = 10
	UnknownErrorStatus            ErrorResponseStatus = 255
)

Knwon values of ErrorResponseStatus

func (ErrorResponseStatus) String

func (e ErrorResponseStatus) String() string

type Feedback

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

Feedback only read binary apple socket

func NewFeedback

func NewFeedback(ctx context.Context, addr string, cert *tls.Certificate) (f *Feedback)

func (*Feedback) Messages

func (f *Feedback) Messages() <-chan *FeedbackMessage

type FeedbackMessage

type FeedbackMessage struct {
	Unsubscribe time.Time
	DeviceToken string
}

type Notification

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

Notification represents a notification

func NewNotification

func NewNotification() *Notification

NewNotification creates a new Notification

func (*Notification) DeviceToken

func (n *Notification) DeviceToken() string

DeviceToken returns the device token

func (*Notification) Encode

func (n *Notification) Encode() ([]byte, error)

Encode encodes a notification packet

func (*Notification) Expiry

func (n *Notification) Expiry() time.Time

Expiry returns the expiry

func (*Notification) HasIdentifier

func (n *Notification) HasIdentifier() bool

HasIdentifier returns whether the Identifier has been set

func (*Notification) Identifier

func (n *Notification) Identifier() NotificationIdentifier

Identifier returns the Identifier

func (*Notification) Payload

func (n *Notification) Payload() Topic

Payload returns the Payload

func (*Notification) Priority

func (n *Notification) Priority() NotificationPriority

Priority returns the Priority

func (*Notification) SetDeviceToken

func (n *Notification) SetDeviceToken(token string)

SetDeviceToken sets the device token. Must be a 64 bytes hex string.

func (*Notification) SetExpiry

func (n *Notification) SetExpiry(expiry time.Time)

SetExpiry sets the expiry. Fractions of seconds are truncated. APNS discards the notification if it wasn't able to send it after this duration. An expiry of 0 means that the notification is discarded immediately by APNS if it can not be sent (the is the default).

func (*Notification) SetIdentifier

func (n *Notification) SetIdentifier(identifier NotificationIdentifier)

SetIdentifier sets a custom identifier. Two notifications sent to the same Sender must have different identifiers. Sender automatically chooses an identifier if one was not set.

func (*Notification) SetPayload

func (n *Notification) SetPayload(payload Topic)

SetPayload sets the Payload.

func (*Notification) SetPriority

func (n *Notification) SetPriority(priority NotificationPriority)

SetPriority sets the priority. The default is ImmediatePriority.

type NotificationIdentifier

type NotificationIdentifier uint32

NotificationIdentifier represents a notification identifier

type NotificationPriority

type NotificationPriority uint8

NotificationPriority represents a notification priority

const (
	// ImmediatePriority sets the push message to be sent immediately. This is
	// the default.
	ImmediatePriority NotificationPriority = 10
	// PowerSavingPriority sets the push message to be sent at a time that conserves power on the device receiving it
	PowerSavingPriority NotificationPriority = 5
)

type Payload

type Payload map[string]interface{}

Payload represents a notification payload

func (Payload) Bytes

func (p Payload) Bytes() ([]byte, error)

ToJSON encodes the Payload to JSON. The encoded payload cannot exceed MaxPayloadLen bytes

func (Payload) Set

func (p Payload) Set(name string, value interface{})

Set sets a custom item outside the aps namespace

func (Payload) SetAlertDictionary

func (p Payload) SetAlertDictionary(alert *AlertDictionary)

SetAlertDictionary sets the alert item as a dictionary

func (Payload) SetAlertString

func (p Payload) SetAlertString(alert string)

SetAlertString sets the alert item as a string

func (Payload) SetBadge

func (p Payload) SetBadge(badge int)

SetBadge sets the badge item

func (Payload) SetSound

func (p Payload) SetSound(sound string)

SetSound sets the sound item

func (Payload) ToJSON

func (p Payload) ToJSON() ([]byte, error)

ToJSON encodes the Payload to JSON. The encoded payload cannot exceed MaxPayloadLen bytes deprecated

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

func (s *Sender) Done() <-chan struct{}

Done returns a channel that's closed when this Sender has terminated (usually after ctx.Done() has been closed).

func (*Sender) Errors

func (s *Sender) Errors() <-chan *SenderError

Errors returns the channel from which to receive SenderErrors

func (*Sender) Notifications

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

Notifications returns the channel to which to send notifications

type SenderError

type SenderError struct {
	Notification  *Notification
	ErrorResponse *ErrorResponse
}

SenderError represents a sender error

type Topic

type Topic interface {
	Bytes() ([]byte, error)
}

Interface for universal notification payload

Jump to

Keyboard shortcuts

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