-
Notifications
You must be signed in to change notification settings - Fork 9
/
getui_test.go
302 lines (246 loc) · 6.1 KB
/
getui_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package getui
import (
"encoding/json"
"errors"
"fmt"
"os"
"testing"
"time"
)
var Cfg = &GetuiConfig{
AppId: "",
AppSecret: "",
AppKey: "",
MasterSecret: "",
}
//测试单推
func TestGeTui_SendByCid(t *testing.T) {
igetui, err := NewGeTui(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cid := "b1f0c722b141e4ee718f2b386b39c683"
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = igetui.SendByCid(cid, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//测试群推
func TestGeTui_SendByCids(t *testing.T) {
igetui, err := NewGeTui(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
cids := []string{"b1f0c722b141e4ee718f2b386b39c683"}
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = igetui.SendByCids(cids, &payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//测试全推
func TestGeTui_SendAll(t *testing.T) {
igetui, err := NewGeTui(Cfg)
if err != nil {
t.Error(err)
os.Exit(1)
}
payLoad := Payload{"这是测试title", "这是测试内容", "1", ""}
err = igetui.SendAll(&payLoad)
if err != nil {
t.Error(err)
} else {
t.Log("ok")
}
}
//配置
type GetuiConfig struct {
AppId string `toml:"app_id"`
AppKey string `toml:"app_key"`
AppSecret string `toml:"app_secret"`
MasterSecret string `toml:"master_secret"`
}
//消息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 GeTuiPush struct {
Config *GetuiConfig
}
//获取个推实例
func NewGeTui(config *GetuiConfig) (*GeTuiPush, error) {
if config.AppId == "" || config.AppSecret == "" || config.AppKey == "" {
return nil, errors.New("请检查配置")
}
gt := &GeTuiPush{
Config: config,
}
return gt, nil
}
//透传模板
func IGtTransmissionTemplate(payload *Payload) (*Transmission, *PushInfo, error) {
payloadByte, err := json.Marshal(payload)
if err != nil {
return nil, nil, err
}
//notify:多厂商推送透传消息带通知配置
//notify := &getui.Notify{
// Title: payload.PushTitle,
// Content: payload.GetPushBody(),
// Intent: "",
// Type: "1",
//}
//实例化透传模板
template := &Transmission{
TransmissionType: false,
TransmissionContent: string(payloadByte), //安卓使用
//Notify: notify,
}
//设置APNS
apn := Apns{
Category: "ACTIONABLE",
}
if payload.IsShowNotify == "1" {
alertmsg := &Alert{}
alertmsg.Title = payload.PushTitle
alertmsg.Body = payload.PushBody
apn.Alert = alertmsg
apn.Sound = ""
apn.AutoBadge = "+1" //角标
apn.ContentAvailable = 0
} else { //静默推送
apn.Sound = "com.gexin.ios.silence"
apn.AutoBadge = "+0" //角标
apn.ContentAvailable = 1
}
//pushInfo
pushInfo := PushInfo{}
pushInfo["aps"] = apn
pushInfo["payload"] = string(payloadByte) //iOS使用
return template, &pushInfo, nil
}
//根据用户cid推送
func (g *GeTuiPush) SendByCid(cid string, payload *Payload) error {
//获取签名
token, _ := GetGeTuiToken(g.Config.AppId, g.Config.AppKey, g.Config.MasterSecret)
//消息体
message := GetMessage()
message.AppKey = g.Config.AppKey
message.MsgType = MsgType.Transmission
//推送模板
template, pushInfo, err := IGtTransmissionTemplate(payload)
if err != nil {
return err
}
pushSingleParam := &PushSingleParam{
Message: message,
Transmission: template,
Cid: cid,
PushInfo: pushInfo,
RequestId: time.Now().Format("20160102150405"),
}
res, err := PushSingle(g.Config.AppId, token, pushSingleParam)
if err != nil {
return err
}
fmt.Println(res)
return nil
}
//根据用户cids批量推送
func (g *GeTuiPush) SendByCids(cids []string, payload *Payload) error {
//获取签名
token, _ := GetGeTuiToken(g.Config.AppId, g.Config.AppKey, g.Config.MasterSecret)
//消息体
message := GetMessage()
message.AppKey = g.Config.AppKey
message.MsgType = MsgType.Transmission
//推送模板
template, pushInfo, err := IGtTransmissionTemplate(payload)
if err != nil {
return err
}
//a. 先调用save_list_body保存消息共同体
saveListBodyParam := &SaveListBodyParam{
Message: message,
Transmission: template,
PushInfo: pushInfo, //必须
}
res, err := SaveListBody(g.Config.AppId, token, saveListBodyParam)
if err != nil {
return err
}
if res.Result != "ok" {
return errors.New(fmt.Sprintf("获取contentId失败:%s,%s", res.Result, res.Desc))
}
taskid := res.TaskId
//fmt.Println("content_id: " + taskid)
//b. 获取到taskid后再调用群推接口推送
pushListParam := &PushListParam{
Cid: cids,
Taskid: taskid,
NeedDetail: true,
}
res2, err := PushList(g.Config.AppId, token, pushListParam)
if err != nil {
return err
}
fmt.Println(res2)
return nil
}
//推送给所有人
func (g *GeTuiPush) SendAll(payload *Payload) error {
//获取签名
token, _ := GetGeTuiToken(g.Config.AppId, g.Config.AppKey, g.Config.MasterSecret)
//消息体
message := GetMessage()
message.AppKey = g.Config.AppKey
message.MsgType = MsgType.Transmission
//推送模板
template, pushInfo, err := IGtTransmissionTemplate(payload)
if err != nil {
return err
}
conditions := Condition{}
conditions = append(conditions, AppCondition{
Key: PHONE_TYPE,
Values: []string{"ANDROID", "IOS"},
})
pushAppParam := &PushAppParam{
Message: message,
Transmission: template,
PushInfo: pushInfo,
Condition: &conditions,
RequestId: time.Now().Format("20160102150405"),
}
res, err := PushApp(g.Config.AppId, token, pushAppParam)
if err != nil {
return err
}
fmt.Println(res)
return nil
}
//获取推送结果接口
func (g *GeTuiPush) GetPushResult(taskidlist []string) (*PushResult, error) {
//获取签名
token, _ := GetGeTuiToken(g.Config.AppId, g.Config.AppKey, g.Config.MasterSecret)
pushParam := &PushResultParam{
Taskidlist: taskidlist,
}
res, err := GetPushResult(g.Config.AppId, token, pushParam)
if err != nil {
return nil, err
}
//fmt.Println(res)
return res, nil
}