forked from sideshow/apns2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
55 lines (45 loc) · 1.18 KB
/
token.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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/chenyuy/apns2"
"github.com/chenyuy/apns2/token"
)
func main() {
authKeyPath := flag.String("authKey", "", "Path to .p8 APNSAuthKey file (Required)")
deviceToken := flag.String("token", "", "Push token (Required)")
topic := flag.String("topic", "", "Topic (Required)")
keyID := flag.String("keyID", "", "APNS KeyID (Required)")
teamID := flag.String("teamID", "", "APNS TeamID (Required)")
flag.Parse()
if *authKeyPath == "" || *deviceToken == "" || *topic == "" || *keyID == "" || *teamID == "" {
flag.PrintDefaults()
os.Exit(1)
}
authKey, err := token.AuthKeyFromFile(*authKeyPath)
if err != nil {
log.Fatal("token error:", err)
}
token := &token.Token{
AuthKey: authKey,
KeyID: *keyID,
TeamID: *teamID,
}
notification := &apns2.Notification{}
notification.DeviceToken = *deviceToken
notification.Topic = *topic
notification.Payload = []byte(`{
"aps" : {
"alert" : "Hello!"
}
}
`)
client := apns2.NewTokenClient(token).Production()
res, err := client.Push(notification)
if err != nil {
log.Fatal("Error:", err)
}
fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}