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 ¶
const MaxPushNotificationBytes = 2048
MaxPushNotificationBytes is the maximum number of bytes permitted in a payload sent to the APNs
const ProductionGateway = "gateway.push.apple.com:2195"
ProductionGateway is the host address and port to pass NewClient for connecting to the production APNs.
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 ¶
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 ¶
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.