-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_to_single.go
64 lines (54 loc) · 1.82 KB
/
push_to_single.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
package getui
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
)
// PushToSingler 推送单个消息到指定客户端
type PushToSingler interface {
// 推送单个消息到指定客户端
PushToSingle(PushToSingleParam) ([]byte, error)
// PushToSingleContext 携带上下文的推送单个消息到指定客户端
PushToSingleContext(context.Context, PushToSingleParam) ([]byte, error)
}
// PushToSingleParam 单推
type PushToSingleParam struct {
RequestID string `json:"requestid"`
Message Message `json:"message"`
Link *Link `json:"link,omitempty"`
Notification *Notification `json:"notification,omitempty"`
Transmission *Transmission `json:"transmission,omitempty"`
PushInfo PushInfo `json:"push_info,omitempty"`
Cid string `json:"cid,omitempty"`
Alias string `json:"alias,omitempty"`
}
// NewPushToSingleParam 返回单推消息参数
func NewPushToSingleParam(message Message) PushToSingleParam {
return PushToSingleParam{
Message: message,
RequestID: strconv.FormatInt(time.Now().UnixNano(), 10),
}
}
// PushToSingle 单推
func (g *Getui) PushToSingle(p PushToSingleParam) ([]byte, error) {
return g.PushToSingleContext(context.Background(), p)
}
// PushToSingleContext 带有上下文的推送
func (g *Getui) PushToSingleContext(ctx context.Context, p PushToSingleParam) ([]byte, error) {
if err := g.RefreshTokenContext(ctx); nil != err {
return nil, err
}
if "" == p.RequestID {
p.RequestID = strconv.FormatInt(time.Now().UnixNano(), 10)
}
url := fmt.Sprintf(`%s/%s/push_single`, APIServer, g.AppID)
body, err := json.Marshal(p)
if nil != err {
return nil, errors.Wrap(err, "encode push_to_single param to json bytes")
}
return SendContext(ctx, url, g.Token, bytes.NewBuffer(body))
}