Skip to content

Commit

Permalink
Add Proxy Example
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshow committed Dec 4, 2019
1 parent 991ef56 commit 5bc667b
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions _example/proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"

"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"golang.org/x/net/http2"
)

func main() {
certPath := flag.String("cert", "", "Path to .p12 certificate file (Required)")
token := flag.String("token", "", "Push token (Required)")
topic := flag.String("topic", "", "Topic (Required)")
proxy := flag.String("proxy", "", "Proxy URL (Required)")
flag.Parse()

if *certPath == "" || *token == "" || *topic == "" {
flag.PrintDefaults()
os.Exit(1)
}

certificate, certErr := certificate.FromP12File(*certPath, "")
if certErr != nil {
log.Fatal("Cert Error:", certErr)
}

tlsConfig := &tls.Config{
Certificates: []tls.Certificate{certificate},
}

if len(certificate.Certificate) > 0 {
tlsConfig.BuildNameToCertificate()
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: func(request *http.Request) (*url.URL, error) {
return url.Parse(*proxy)
},
IdleConnTimeout: 60 * time.Second,
}

transportErr := http2.ConfigureTransport(transport)
if transportErr != nil {
log.Fatal("Transport Error:", transportErr)
}

client := &apns2.Client{
HTTPClient: &http.Client{
Transport: transport,
Timeout: apns2.HTTPClientTimeout,
},
Certificate: certificate,
Host: apns2.DefaultHost,
}

notification := &apns2.Notification{}
notification.DeviceToken = *token
notification.Topic = *topic
notification.Payload = []byte(`{
"aps" : {
"alert" : "Hello!"
}
}
`)

res, err := client.Push(notification)

if err != nil {
log.Fatal("Error:", err)
}

fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}

0 comments on commit 5bc667b

Please sign in to comment.