-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_alert.go
82 lines (63 loc) · 1.86 KB
/
send_alert.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
// 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 (
AlertTextFlag = "alert-text"
AlertTextDefault = ""
AlertTextDesc = "alert text"
BadgeCountFlag = "badge-count"
BadgeCountDefault = 0
BadgeCountDesc = "badge count"
SoundNameFlag = "sound-name"
SoundNameDefault = ""
SoundNameDesc = "sound name"
)
type SendAlertCmd struct {
SendCmd
AlertText string
BadgeCount int
SoundName string
}
func NewSendAlertCommand() *cobra.Command {
cmd := &SendAlertCmd{}
cobraCmd := &cobra.Command{
Use: "alert",
Short: "Send simple alert 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.StringVar(&cmd.AlertText, AlertTextFlag, AlertTextDefault, AlertTextDesc)
flags.IntVar(&cmd.BadgeCount, BadgeCountFlag, BadgeCountDefault, BadgeCountDesc)
flags.StringVar(&cmd.SoundName, SoundNameFlag, SoundNameDefault, SoundNameDesc)
_ = cobraCmd.MarkFlagRequired(AppIdFlag)
_ = cobraCmd.MarkFlagRequired(DeviceTokenFlag)
return cobraCmd
}
func (cmd *SendAlertCmd) Run() error {
notificationBuilder := apns.NewNotificationBuilder(cmd.AppId)
if cmd.AlertText != "" {
notificationBuilder.SetAlertText(cmd.AlertText)
}
if cmd.BadgeCount != -1 {
notificationBuilder.SetBadgeCount(cmd.BadgeCount)
}
if cmd.SoundName != "" {
notificationBuilder.SetSoundName(cmd.SoundName)
}
headers, content, err := notificationBuilder.Build()
if err != nil {
return err
}
return cmd.sendNotification(headers, content)
}