-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_raw.go
72 lines (55 loc) · 1.68 KB
/
send_raw.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
// Copyright 2019 Brannon Jones. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package send
import (
"github.com/brannon/apnstool/apns"
"github.com/brannon/apnstool/cmdio"
"github.com/spf13/cobra"
)
const (
PriorityFlag = "priority"
PriorityDefault = ""
PriorityDesc = "value for 'apns-priority' header"
PushTypeFlag = "push-type"
PushTypeDefault = ""
PushTypeDesc = "value for 'apns-push-type' header"
)
type SendRawCmd struct {
SendCmd
DataString string
Priority string
PushType string
}
func NewSendRawCommand() *cobra.Command {
cmd := &SendRawCmd{}
cobraCmd := &cobra.Command{
Use: "raw",
Short: "Send raw notification through APNs",
RunE: func(c *cobra.Command, args []string) error {
cmd.Client = apns.NewClient()
cmd.IO = cmdio.NewCmdIO(c.OutOrStdout())
return cmd.Run()
},
}
flags := cobraCmd.Flags()
BindSendCommonFlags(flags, &cmd.SendCmd)
flags.StringVarP(&cmd.DataString, DataStringFlag, DataStringShortFlag, DataStringDefault, DataStringDesc)
flags.StringVar(&cmd.Priority, PriorityFlag, PriorityDefault, PriorityDesc)
flags.StringVar(&cmd.PushType, PushTypeFlag, PushTypeDefault, PushTypeDesc)
_ = cobraCmd.MarkFlagRequired(AppIdFlag)
_ = cobraCmd.MarkFlagRequired(DeviceTokenFlag)
_ = cobraCmd.MarkFlagRequired(DataStringFlag)
return cobraCmd
}
func (cmd *SendRawCmd) Run() error {
headers := make(apns.Headers)
headers["apns-topic"] = cmd.AppId
if cmd.Priority != "" {
headers["apns-priority"] = cmd.Priority
}
if cmd.PushType != "" {
headers["apns-push-type"] = cmd.PushType
}
return cmd.sendNotification(headers, []byte(cmd.DataString))
}