forked from sideshow/apns2
/
main.go
67 lines (54 loc) · 1.47 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
package main
import (
"fmt"
"log"
"os"
"flag"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload"
)
func main() {
certPath := flag.String("cert", "", "Path to .p12 certificate file (Required)")
count := flag.Int("count", 200, "Number of pushes to send")
token := flag.String("token", "", "Push token (Required)")
topic := flag.String("topic", "", "Topic (Required)")
flag.Parse()
if *certPath == "" || *token == "" || *topic == "" {
flag.PrintDefaults()
os.Exit(1)
}
cert, err := certificate.FromP12File(*certPath, "")
if err != nil {
log.Fatal("Cert Error:", err)
}
notifications := make(chan *apns2.Notification, 100)
responses := make(chan *apns2.Response, *count)
client := apns2.NewClient(cert).Production()
for i := 0; i < 50; i++ {
go worker(client, notifications, responses)
}
for i := 0; i < *count; i++ {
n := &apns2.Notification{
DeviceToken: *token,
Topic: *topic,
Payload: payload.NewPayload().Alert(fmt.Sprintf("Hello! %v", i)),
}
notifications <- n
}
for i := 0; i < *count; i++ {
res := <-responses
fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}
close(notifications)
close(responses)
}
func worker(client *apns2.Client, notifications <-chan *apns2.Notification, responses chan<- *apns2.Response) {
for n := range notifications {
res, err := client.Push(n)
if err != nil {
log.Fatal("Push Error:", err)
}
responses <- res
}
}