func main() { var deviceToken, filename, password, environment string flag.StringVar(&deviceToken, "d", "", "Device token") flag.StringVar(&filename, "c", "", "Path to p12 certificate file") flag.StringVar(&password, "p", "", "Password for p12 file.") flag.StringVar(&environment, "e", "development", "Environment") flag.Parse() cert, key, err := certificate.Load(filename, password) if err != nil { log.Fatal(err) } service := push.Service{ Client: push.NewClient(certificate.TLS(cert, key)), Host: push.Development, } if environment == "production" { service.Host = push.Production } p := payload.APS{ Alert: payload.Alert{Body: "Hello HTTP/2"}, Badge: badge.New(42), } id, err := service.Push(deviceToken, &push.Headers{}, p) if err != nil { log.Fatal(err) } log.Println("apns-id:", id) }
func send(token string, headers *push.Headers, payload interface{}, service *push.Service) { if token != "" { println("Sending push: ", token) b, err := json.Marshal(payload) if err != nil { println("Cannot parse payload") os.Exit(1) } id, err := service.Push(token, headers, b) if err != nil { log.Fatal(err, id) } else { println("Push sent successfully ", id) } } }