apns

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

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

Go to latest
Published: Sep 2, 2015 License: Apache-2.0 Imports: 11 Imported by: 0

README

This project is no longer maintained. Check out anachronistic/apns instead.

Introduction

This Go package provides a client that can establish a connection with the Apple Push Notification service and deliver notifications to end users based on a device token. This package has been implemented according to Apple's documentation (available at ever changing/breaking URLs).

GoDoc

Installation

go get github.com/arashpayan/apns

Usage

Create a Client and connect to the APNs

client := apns.NewClient(apns.SandboxGateway, // or apns.ProductionGateway
    "/path/to/my/signed/cert/file.crt",
    "/path/to/my/private/key/file.key",
    func(invalidToken string) {
        // called when we try to send a notification to an invalid token
        // You should delete the token from your db by disassociating it with
        // the user you tried sending the notification too. When this is called
        // it means the person either uninstalled your app or has switched to
        // a new device.
    })
err := client.Connect()
if err != nil {
    // handle the connection error
}

Create a Notification

n := apns.NewNotification(aDeviceToken)
n.Alert = "Hello, user!"
n.Badge = 1
// optionally, include app data ('aps' element of notification json)
n.AppData = map[string]interface{}{"sound":"tada", "Foo":"Bar"}

Send the Notification

client.Send(n)

Documentation

Overview

Package apns provides a client for using the Apple Push Notification service.

Package apns provides a client for using the Apple Push Notification service. To start using it, all you need is your signed APNs certificate from Apple, and your private key.

Using

To interact with the APNs, you must create a Client and connect it to the appropriate gateway. While your developing your Go app, you probably want to use the SandboxGateway.

client := apns.NewClient(apns.SandboxGateway, // or apns.ProductionGateway
    "/path/to/my/signed/cert/file.crt",
    "/path/to/my/private/key/file.key",
    func(invalidToken string) {
        // called when we try to send a notification to an invalid token
        // You should delete the token from your db by disassociating it with
        // the user you tried sending the notification too. When this is called
        // it means the person either uninstalled your app or has switched to
        // a new device.
    })
err := client.Connect()
if err != nil {
    // handle the connection error
}

After successfully connecting to an APNs gateway, create a Notification:

n := apns.NewNotification(aDeviceToken)
n.Alert = "Hello, user!"
n.Badge = 1
// optionally, include app data ('aps' element of notification json)
n.AppData = map[string]interface{}{"sound":"tada", "Foo":"Bar"}

Finally, send the Notification via the Client:

client.Send(n)

Index

Constants

View Source
const MaxPushNotificationBytes = 2048

MaxPushNotificationBytes is the maximum number of bytes permitted in a payload sent to the APNs

View Source
const ProductionGateway = "gateway.push.apple.com:2195"

ProductionGateway is the host address and port to pass NewClient for connecting to the production APNs.

View Source
const SandboxGateway = "gateway.sandbox.push.apple.com:2195"

SandboxGateway is the host address and port to pass NewClient for connecting to the development environment for the APNs.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Gateway         string
	CertificateFile string
	KeyFile         string

	IsConnected bool
	// contains filtered or unexported fields
}

Client is the broker between an APNs provider and the gateway

func NewClient

func NewClient(gateway, certFile, keyFile string, invalidTokenHandler func(string)) *Client

NewClient initializes a Client struct that you can use to send Notifications to the APNs. gateway should be either ProductionGateway or SandboxGateway. certFile and keyFile are paths to your Apple signed certificate and private key. invalidTokenHandler is a function that will be called when an attempt send a notification results in an invalid token error from the APNs. When you receive these errors, you should remove/disassociate the device token from your database/user. This function callback is provided as a simpler alternative to implementing a feedback service that would periodically poll Apple for invalid tokens.

func (*Client) Connect

func (c *Client) Connect() error

Connect establishes a connection to the APNs gateway specified in NewClient(). This method blocks until the connection is established.

func (*Client) Send

func (c *Client) Send(n *Notification)

Send queues a notification for sending to the APNs

type Notification

type Notification struct {
	Alert   string      `json:"alert"`
	Badge   int16       `json:"badge"`
	Sound   string      `json:"sound"`
	AppData interface{} `json:"-"`
	// contains filtered or unexported fields
}

Notification represents a push notification for a specific iOS device

func NewNotification

func NewNotification(devToken string) *Notification

NewNotification creates an APNs notification that can be delivered to an iOS device with the specified devToken.

Jump to

Keyboard shortcuts

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