-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_background.go
62 lines (47 loc) · 1.4 KB
/
send_background.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
// 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"
)
type SendBackgroundCmd struct {
SendCmd
DataString string
}
func NewSendBackgroundCommand() *cobra.Command {
cmd := &SendBackgroundCmd{}
cobraCmd := &cobra.Command{
Use: "background",
Short: "Send simple background 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)
_ = cobraCmd.MarkFlagRequired(AppIdFlag)
_ = cobraCmd.MarkFlagRequired(DeviceTokenFlag)
return cobraCmd
}
func (cmd *SendBackgroundCmd) Run() error {
notificationBuilder := apns.NewNotificationBuilder(cmd.AppId)
notificationBuilder.SetContentAvailable(true)
if cmd.DataString != "" {
data, err := parseDataString(cmd.DataString)
if err != nil {
return err
}
notificationBuilder.Merge(data)
}
headers, content, err := notificationBuilder.Build()
if err != nil {
return err
}
return cmd.sendNotification(headers, content)
}