forked from RobotsAndPencils/buford
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (65 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/RobotsAndPencils/buford/certificate"
"github.com/RobotsAndPencils/buford/payload"
"github.com/RobotsAndPencils/buford/payload/badge"
"github.com/RobotsAndPencils/buford/push"
)
func main() {
var deviceToken, filename, password, environment, host 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()
// ensure required flags are set:
halt := false
if deviceToken == "" {
fmt.Println("Device token is required.")
halt = true
}
if filename == "" {
fmt.Println("Path to .p12 certificate file is required.")
halt = true
}
switch environment {
case "development":
host = push.Development
case "production":
host = push.Production
default:
fmt.Println("Environment can be development or production.")
halt = true
}
if halt {
flag.Usage()
os.Exit(2)
}
// load a certificate and use it to connect to the APN service:
cert, err := certificate.Load(filename, password)
exitOnError(err)
client, err := push.NewClient(cert)
exitOnError(err)
service := push.NewService(client, host)
// construct a payload to send to the device:
p := payload.APS{
Alert: payload.Alert{Body: "Hello HTTP/2"},
Badge: badge.New(42),
}
b, err := json.Marshal(p)
exitOnError(err)
// push the notification:
id, err := service.Push(deviceToken, nil, b)
exitOnError(err)
fmt.Println("apns-id:", id)
}
func exitOnError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}