Skip to content

Instantly share code, notes, and snippets.

@closer
Created March 11, 2020 02:59
Show Gist options
  • Save closer/b873b845fed1a993380dc19d5a7dffa0 to your computer and use it in GitHub Desktop.
Save closer/b873b845fed1a993380dc19d5a7dffa0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"os"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/token"
)
func main() {
authKeyFilePath := os.Args[1]
keyID := os.Args[2]
teamID := os.Args[3]
authKey, err := token.AuthKeyFromFile(authKeyFilePath)
if err != nil {
log.Fatal("token error:", err)
}
fmt.Printf("%+v\n", authKey)
token := &token.Token{
AuthKey: authKey,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID: keyID,
// TeamID from developer account (View Account -> Membership)
TeamID: teamID,
}
notification := &apns2.Notification{}
notification.DeviceToken = os.Args[4]
notification.Topic = os.Args[5]
notification.PushType = apns2.PushTypeAlert
notification.Payload = []byte(`{"aps":{"alert":"Hello!"}}`) // See Payload section below
fmt.Printf("%+v\n", notification)
client := apns2.NewTokenClient(token)
res, err := client.Push(notification)
if err != nil {
log.Fatal("Error:", err)
}
fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment