/
main.go
140 lines (127 loc) · 3.24 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/codegangsta/cli"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/token"
)
func send(notification *apns2.Notification, client *apns2.Client) {
res, err := client.Push(notification)
if err != nil {
log.Fatal(err, res)
} else {
fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}
}
func main() {
var team string
var key string
var topic string
var payloadString string
var payloadFile string
var tokensFile string
app := cli.NewApp()
app.Name = "thunderstorm"
// app.EnableBashCompletion = true
app.Usage = "push TOKEN [...]"
app.Version = "0.3.0"
app.Commands = []cli.Command{
{
Name: "push",
Usage: "Sends an Apple Push Notification to specified devices",
Flags: []cli.Flag{
cli.StringFlag{
Name: "team, t",
Value: "",
Usage: "Team ID",
Destination: &team,
}, cli.StringFlag{
Name: "bundle, b",
Value: "",
Usage: "Topic (bundle-id)",
Destination: &topic,
}, cli.StringFlag{
Name: "key, k",
Value: "",
Usage: "Key ID",
Destination: &key,
},
cli.StringFlag{
Name: "payload, P",
Usage: "JSON payload for notifications",
Destination: &payloadString,
},
cli.StringFlag{
Name: "payload-file, f",
Usage: "Path of JSON file containing the payload to be sent",
Destination: &payloadFile,
},
cli.StringFlag{
Name: "tokens-path, tp",
Usage: "Path of JSON file containing the tokens",
Destination: &tokensFile,
},
},
Action: func(c *cli.Context) error {
deviceToken := c.Args().First()
authKey, err := token.AuthKeyFromFile("AuthKey_" + key + ".p8")
if err != nil {
log.Fatal("token error:", err)
}
token := &token.Token{
AuthKey: authKey,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID: key,
// TeamID from developer account (View Account -> Membership)
TeamID: team,
}
client := apns2.NewTokenClient(token)
if payloadFile != "" {
buffer, err := ioutil.ReadFile(payloadFile)
if err == nil {
payloadString = string(buffer)
}
}
var p map[string]interface{}
if payloadString != "" {
error := json.Unmarshal([]byte(payloadString), &p)
if error != nil {
log.Fatal("Cannot parse payload")
return err
}
}
notification := &apns2.Notification{}
notification.Topic = topic
notification.Payload = payloadString
tokensString := ""
if tokensFile != "" {
buffer, err := ioutil.ReadFile(tokensFile)
if err == nil {
tokensString = string(buffer)
}
var tokens []string
if tokensString != "" {
error := json.Unmarshal([]byte(tokensString), &tokens)
if error != nil {
log.Fatal("Cannot parse tokens array", error)
return error
}
for _, token := range tokens {
notification.DeviceToken = token
send(notification, client)
}
}
} else {
notification.DeviceToken = deviceToken
send(notification, client)
}
return nil
},
},
}
app.Run(os.Args)
}