Skip to content

kwillick/apns

 
 

Repository files navigation

apns

This a fork of anachronistic/apns that adds a persistent connection to the specified gateway and non-blocking sending of notifications.

Usage

Connecting to a gateway and sending a notification

package main

import (
  "fmt"
  apns "github.com/kwillick/apns"
  "time"
)

func main() {
  connection, connErr := apns.NewConnection(apns.AppleDevelopmentGateway, "YOUR_CERT_FILE.pem", "YOUR_KEY_FILE.pem")
  if connErr != nil {
      connection.Close()
      panic(connErr)
  }

  errorChannel := make(chan *apns.PushNotificationError)
  err = connection.Connect(errorChannel)
  if err != nil {
      conn.Close()
	  panic(err)
  }

  // Listen for errors and panic if an error occurs
  go func() {
      err := <-errorChannel
      panic(err)
  }()

  fakeDeviceToken := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  connection.SendBasicNotification(fakeDeviceToken, "Some alert text", "bingbong.aiff", 1, nil)

  // Sleep, since SendNotification doesn't block
  time.Sleep(time.Duration(2) * time.Second)
}

Checking the feedback service

package main

import (
  "fmt"
  apns "github.com/kwillick/apns"
  "os"
)

func main() {
  fmt.Println("- connecting to check for deactivated tokens")

  client := apns.NewFeedbackConnection(apns.AppleFeedbackDevelopmentGateway, "YOUR_CERT_FILE.pem", "YOUR_KEY_FILE.pem")
  go client.ListenForFeedback()

  for {
    resp := <-apns.FeedbackChannel
    fmt.Println("- recv'd:", resp.DeviceToken)
  }
}

About

A Golang package for sending Apple Push Notifications and using the Feedback Service.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%