-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.go
250 lines (225 loc) · 8 KB
/
push.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
package jpush
import (
"bytes"
"encoding/json"
"strconv"
)
// Platform define platform entry
type Platform struct {
isAll bool
Platforms []string
}
// SetAll set isAll value
func (p *Platform) SetAll(all bool) {
p.isAll = all
}
// UnmarshalJSON unmarshal json
func (p *Platform) UnmarshalJSON(data []byte) error {
if string(data) == "all" {
p.isAll = true
return nil
}
p.isAll = false
return json.Unmarshal(data, &p.Platforms)
}
// MarshalJSON marshal json
func (p *Platform) MarshalJSON() (data []byte, err error) {
if p.isAll {
return []byte(`"all"`), nil
}
return json.Marshal(p.Platforms)
}
// Audience define Audience
type Audience struct {
Tag []string `json:"tag,omitempty"`
TagAnd []string `json:"tag_and,omitempty"`
TagNot []string `json:"tag_not,omitempty"`
Alias []string `json:"alias,omitempty"`
RegistrationID []string `json:"registration_id,omitempty"`
Segment []string `json:"segment,omitempty"`
ABTest []string `json:"abtest,omitempty"`
}
// PushAudience define audience entry
type PushAudience struct {
isAll bool
Aud *Audience
}
// SetAll set isAll
func (p *PushAudience) SetAll(all bool) {
p.isAll = all
}
// UnmarshalJSON unmarshal json
func (p *PushAudience) UnmarshalJSON(data []byte) error {
if string(data) == "all" {
p.isAll = true
return nil
}
p.isAll = false
return json.Unmarshal(data, p.Aud)
}
// MarshalJSON marshal json
func (p *PushAudience) MarshalJSON() (data []byte, err error) {
if p.isAll {
return []byte(`"all"`), nil
}
return json.Marshal(p.Aud)
}
// PushNotification define notification
type PushNotification struct {
Alert string `json:"alert,omitempty"`
Android *NotificationAndroid `json:"android,omitempty"`
IOS *NotificationIOS `json:"ios,omitempty"`
WinPhone *NotificationWinPhone `json:"winphone,omitempty"`
}
// NotificationAndroid define android notification
type NotificationAndroid struct {
Alert string `json:"alert"`
Title string `json:"title,omitempty"`
BuilderID int `json:"builder_id,int,omitempty"`
Priority int `json:"priority,omitempty"`
Category string `json:"category,omitempty"`
Style int `json:"style,int,omitempty"`
AlertType int `json:"alert_type,int,omitempty"`
BigText string `json:"big_text,omitempty"`
Inbox map[string]interface{} `json:"inbox,omitempty"`
BigPicPath string `json:"big_pic_path,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
LargeIcon string `json:"large_icon,omitempty"`
Intent map[string]interface{} `json:"intent,omitempty"`
ChannelID string `json:"channel_id,omitempty"` // 通知栏消息分类 【参考文档:https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push#android】
}
// NotificationIOS define ios notification
type NotificationIOS struct {
Alert interface{} `json:"alert"`
Sound string `json:"sound,omitempty"`
Badge int `json:"badge,int,omitempty"`
ContentAvailable bool `json:"content-available,omitempty"`
MutableContent bool `json:"mutable-content,omitempty"`
Category string `json:"category,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
}
// NotificationWinPhone define winphone notification
type NotificationWinPhone struct {
Alert string `json:"alert"`
Title string `json:"title,omitempty"`
OpenPage string `json:"_open_page,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
}
// PushMessage define push message
type PushMessage struct {
MsgContent string `json:"msg_content"`
Title string `json:"title,omitempty"`
ContentType string `json:"content_type,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
}
// SmsMessage define sms message
type SmsMessage struct {
DelayTime int `json:"delay_time,int"`
TempID float64 `json:"temp_id,float"`
TempPara map[string]interface{} `json:"temp_para,omitempty"`
}
// PushOptions define options
type PushOptions struct {
SendNo int `json:"sendno,int,omitempty"`
TimeToLive int `json:"time_to_live,int,omitempty"`
OverrideMsgID int64 `json:"override_msg_id,int64,omitempty"`
ApnsProduction bool `json:"apns_production"`
ApnsCollapseID string `json:"apns_collapse_id,omitempty"`
BigPushDuration int `json:"big_push_duration,int,omitempty"`
ThirdPartyChannel map[string]interface{} `json:"third_party_channel,omitempty"` // 推送请求下发通道 【仅针对配置了厂商用户使用有效,详情参考:https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push#third_party_channel-%E8%AF%B4%E6%98%8E】
Classification int `json:"classification,int,omitempty"` // 消息类型分类: 0:代表运营消息 1:代表系统消息【优先级最高会覆盖,options.third_party_channel.vivo.classification third_party_channel 字段设置的值;极光不对指定的消息类型进行判断或校准,会以开发者自行指定的消息类型适配 Android 厂商通道。不填默认为 0】
}
// PushRequest define push request body
type PushRequest struct {
Cid string `json:"cid,omitempty"`
Platform *Platform `json:"platform"`
Audience *PushAudience `json:"audience"`
Notification *PushNotification `json:"notification,omitempty"`
Message *PushMessage `json:"message,omitempty"`
SmsMessage *SmsMessage `json:"sms_message,omitempty"`
Options *PushOptions `json:"options,omitempty"`
}
// PushResponse define push repsone
type PushResponse struct {
MsgID string `json:"msg_id"`
Sendno string `json:"sendno"`
}
// PushCIDResponse get cid response
type PushCIDResponse struct {
Cids []string `json:"cidlist"`
}
// Push push notification or message to devices
// POST /v3/push
func (j *JPush) Push(req *PushRequest) (*PushResponse, error) {
url := j.GetURL("push") + "push"
buf, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := j.request("POST", url, bytes.NewReader(buf), nil)
if err != nil {
return nil, err
}
ret := new(PushResponse)
err = json.Unmarshal(resp, ret)
if err != nil {
return nil, err
}
return ret, nil
}
// PushGetCid get push by cid
// GET /v3/push/cid[?count=n[&type=xx]]
func (j *JPush) PushGetCid(count int, cidtype string) (*PushCIDResponse, error) {
url := j.GetURL("push") + "push/cid"
params := make(map[string]string)
params["count"] = strconv.Itoa(count)
params["type"] = cidtype
resp, err := j.request("GET", url, nil, params)
if err != nil {
return nil, err
}
ret := new(PushCIDResponse)
err = json.Unmarshal(resp, ret)
if err != nil {
return nil, err
}
return ret, nil
}
// GroupPush group push
// POST /v3/grouppush
func (j *GroupPush) GroupPush(req *PushRequest) (*PushResponse, error) {
url := j.GetURL("push") + "grouppush"
buf, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := j.request("POST", url, bytes.NewReader(buf), nil)
if err != nil {
return nil, err
}
ret := new(PushResponse)
err = json.Unmarshal(resp, ret)
if err != nil {
return nil, err
}
return ret, nil
}
// PushValidate push validate, not real push
// POST /v3/push/validate
func (j *JPush) PushValidate(req *PushRequest) (*PushResponse, error) {
url := j.GetURL("push") + "push/validate"
buf, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := j.request("POST", url, bytes.NewReader(buf), nil)
if err != nil {
return nil, err
}
ret := new(PushResponse)
err = json.Unmarshal(resp, ret)
if err != nil {
return nil, err
}
return ret, nil
}