-
Notifications
You must be signed in to change notification settings - Fork 1
/
hwpush_test.go
165 lines (138 loc) · 3.2 KB
/
hwpush_test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package huawei
import (
"encoding/json"
"errors"
"fmt"
"os"
"testing"
)
var Cfg = &HwpushConfig{
AppId: "",
AppSecret: "",
Package: "",
}
//测试单推
func TestHwPush_SendByCid(t *testing.T) {
hwpush, err := NewHuawei(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cid := "xxx"
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = hwpush.SendByCid(cid, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//测试群推
func TestHwPush_SendByCids(t *testing.T) {
hwpush, err := NewHuawei(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cids := []string{"xxx"}
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = hwpush.SendByCids(cids, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
type HwpushConfig struct {
AppId string `toml:"app_id"`
AppSecret string `toml:"app_secret"`
Package string `toml:"package"`
}
//消息payload,根据业务自定义
type Payload struct {
PushTitle string `json:"push_title"`
PushBody string `json:"push_body"`
IsShowNotify string `json:"is_show_notify"`
Ext string `json:"ext"`
}
type HuaweiPush struct {
Config *HwpushConfig
}
//存储每个cid失败的原因
type PushSendResultMsg struct {
Success int64 `json:"success"`
Failure int64 `json:"failure"`
IllegalTokens []string `json:"illegal_tokens"`
}
//获取实例
func NewHuawei(config *HwpushConfig) (*HuaweiPush, error) {
if config.AppId == "" || config.Package == "" || config.AppSecret == "" {
return nil, errors.New("请检查配置")
}
hwpush := &HuaweiPush{
Config: config,
}
return hwpush, nil
}
//根据用户cid推送
func (h *HuaweiPush) SendByCid(cid string, payload *Payload) error {
cids := []string{cid}
return h.SendByCids(cids, payload)
}
//组装消息体
func NewMessage(appPkgName string, payload *Payload) *Message {
msgType := 1 //默认透传
if payload.IsShowNotify == "1" { //通知栏
msgType = 3
}
payload_str, _ := json.Marshal(payload)
return &Message{
Hps: Hps{
Msg: Msg{
Type: msgType, //1, 透传异步消息; 3, 系统通知栏异步消息。注意:2和4以后为保留后续扩展使用
Body: Body{
Content: payload.PushBody,
Title: payload.PushTitle,
},
Action: Action{
Type: 3, //1, 自定义行为; 2, 打开URL; 3, 打开App;
Param: Param{
AppPkgName: appPkgName,
},
},
},
Ext: Ext{
Customize: []string{string(payload_str)},
},
},
}
}
//根据用户cids批量推送
func (h *HuaweiPush) SendByCids(cids []string, payload *Payload) error {
//nspCtx
vers := &Vers{
Ver: "1",
AppID: h.Config.AppId,
}
nspCtx, _ := json.Marshal(vers)
//hps
hps, err := json.Marshal(NewMessage(h.Config.Package, payload))
if err != nil {
return err
}
deviceToken, err := json.Marshal(cids)
if err != nil {
return err
}
pushSendParam := &PushSendParam{
DeviceToken: string(deviceToken),
NspCtx: string(nspCtx),
Payload: string(hps),
}
pushSendResult, err, bizErr := PushSend(h.Config.AppId, h.Config.AppSecret, pushSendParam)
if err != nil {
return err
}
fmt.Println(pushSendResult, bizErr)
return nil
}