apns

package module
v0.0.0-...-1abf39e Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2013 License: BSD-3-Clause Imports: 12 Imported by: 0

README

Go APNs Package

This Go package implements the Apple Push Notification System (APNs) binary interface.

Usage

This package provides simple interfaces for establishing authenticated connections to APNs gateways and sending notifications. The function apns.DialAPN(...) returns a net.Conn which is authenticated and ready to receive data. Notifications implement the io.Writer and io.Reader interfaces so that sending a notification is done by having it write to a connection.

var notif = `
{
	"command": 2,
	"device-token": "beefca5e",
	"identifier": 1,
	"expiry": 0,
	"priority": 10,
	"payload": {
		"aps" : {
			"content-available": 1,
   	    	"alert" : "Hello World",
   			"badge" : 42
		}
	}
}`

func main() {
	cert, _ := apns.LoadPemFile("notifyme_cert.pem") // Load the pem file from the current dir.
	conn, _ := apns.DialAPN(&cert, apns.SANDBOX, false)
	defer conn.Close()

	n := apns.MakeNotification([]byte(notif))
	n.WriteTo(conn)
}

Other Go implementations of APNs:

Other non-Go APNs projects:

apnsend

The apnsend utility is a command line tool which uses the apns package for sending push notifications.

apnserver

[note: this is probably broken right now] The apnserver utility will respond to the APNs protocol with mock data. The server can be configured to a specific mock failure rate to simulate errors and dropped connections.

License

Distribution and use of this project is governed by the [3-clause] Revised BSD license that can be found in the LICENSE file.

Documentation

Overview

Package apns implements the Apple Push Notification System (APNS) binary interface.

Index

Constants

This section is empty.

Variables

View Source
var UnknwonCommandErr = errors.New("Unknown command ID.")

UnknwonCommandErr error is used when APN data is encountered with a command that is unknown.

Functions

func Dial

func Dial(cer *tls.Certificate, host string, delay bool) (net.Conn, error)

Dial will connect to an APNs server provided in the host parameter. Unless you plan on using a non-standard APNs server (like a mock server) then it's preferable to use DialAPN or DialFeedback.

func DialAPN

func DialAPN(cer *tls.Certificate, env Environment, delay bool) (net.Conn, error)

DialAPN will create a TCP connection to Apple's APNs server using the certificate provided. The delay parameter tells the network stack to use Nagle's algorithm to batch data in TCP packets.

func DialFeedback

func DialFeedback(cer *tls.Certificate, env Environment) (net.Conn, error)

DialFeedback will create a TCP connection to Apple's feedback service.

func LoadPem

func LoadPem(pemBlock []byte) (cert tls.Certificate, err error)

LoadPem is similar to tls.X509KeyPair found in tls.go except that this function reads all blocks from the same file.

func LoadPemFile

func LoadPemFile(pemFile string) (cert tls.Certificate, err error)

LoadPemFile reads a combined certificate+key pem file into memory.

Types

type Environment

type Environment int8

Environment represents an APNs production or sandbox environment configuration for connections.

From the Local and Push Notification Programming Guide:

The binary interface of the production environment is available
through gateway.push.apple.com, port 2195; the binary interface of
the sandbox (development) environment is available through
gateway.sandbox.push.apple.com, port 2195. You may establish
multiple, parallel connections to the same gateway or to multiple
gateway instances.
const (
	DISTRIBUTION Environment = iota
	SANDBOX      Environment = iota
)

type Packet

type Packet interface {
	ReadFrom(r io.Reader) error
	String() string
	WriteTo(w io.Writer) error
}

Packet represents the various data formats that may be encountered when communicating with APNs.

func ReadCommand

func ReadCommand(r io.Reader) (p Packet, err error)

ReadCommand will read an APNs data format from an input stream and return a Packet if successful.

type PushNotification

type PushNotification interface {
	PushNotification()
	ReadFrom(r io.Reader) error
	WriteTo(w io.Writer) error
	String() string
}

Notification represents a specific set of APNs packets which are used for delivering push notifications.

func MakeNotification

func MakeNotification(data []byte) (pn PushNotification)

Directories

Path Synopsis
Utility for sending push notifications using the Apple's Push Notification System (APNs) Go library.
Utility for sending push notifications using the Apple's Push Notification System (APNs) Go library.

Jump to

Keyboard shortcuts

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