forked from sideshow/apns2
/
main.go
82 lines (68 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/thucnq/apns2"
"github.com/thucnq/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)
}