forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_push_helper.go
262 lines (221 loc) · 6.1 KB
/
publish_push_helper.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
package pubnub
// PNMPNSData is the struct used for the MPNS paylod
type PNMPNSData struct {
Title string `json:"title"`
Type string `json:"type"`
Count int `json:"count"`
BackTitle string `json:"back_title"`
BackContent string `json:"back_content"`
Custom map[string]interface{}
}
// PNFCMData is the struct used for the FCM paylod
type PNFCMData struct {
Data PNFCMDataFields `json:"data"`
Custom map[string]interface{}
}
// PNFCMDataFields is the helper struct used for the FCM paylod
type PNFCMDataFields struct {
Summary interface{} `json:"summary"`
Custom map[string]interface{}
}
// PNAPSData is the helper struct used for the APNS paylod
type PNAPSData struct {
Alert interface{} `json:"alert"`
Badge int `json:"badge"`
Sound string `json:"sound"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Body string `json:"body"`
Custom map[string]interface{}
}
// PNAPNSData is the struct used for the APNS paylod
type PNAPNSData struct {
APS PNAPSData `json:"aps"`
Custom map[string]interface{}
}
// PNAPNS2Data is the struct used for the APNS2 paylod
type PNAPNS2Data struct {
CollapseID string `json:"collapseId"`
Expiration string `json:"expiration"`
Targets []PNPushTarget `json:"targets"`
Version string `json:"version"`
}
// PNPushTarget is the helper struct used for the APNS2 paylod
type PNPushTarget struct {
Topic string `json:"topic"`
ExcludeDevices []string `json:"exclude_devices"`
Environment PNPushEnvironment `json:"environment"`
}
type publishPushHelperBuilder struct {
opts *publishPushHelperOpts
}
func newPublishPushHelperBuilder(pubnub *PubNub) *publishPushHelperBuilder {
builder := publishPushHelperBuilder{
opts: &publishPushHelperOpts{
pubnub: pubnub,
},
}
return &builder
}
func newPublishPushHelperBuilderWithContext(pubnub *PubNub,
context Context) *publishPushHelperBuilder {
builder := publishPushHelperBuilder{
opts: &publishPushHelperOpts{
pubnub: pubnub,
ctx: context,
},
}
return &builder
}
// BuildPayload builds the push payload and returns an map of interface
func (b *publishPushHelperBuilder) BuildPayload() map[string]interface{} {
response := make(map[string]interface{})
apns := b.opts.buildAPNSPayload()
if apns != nil {
response["pn_apns"] = apns
apns2 := b.opts.PushAPNS2Data
if apns2 != nil {
response["pn_push"] = apns2
}
}
mpns := b.opts.buildMPNSPayload()
if mpns != nil {
response["pn_mpns"] = mpns
}
fcm := b.opts.buildFCMPayload()
if fcm != nil {
response["pn_gcm"] = fcm
}
if b.opts.CommonPayload != nil {
for key, value := range b.opts.CommonPayload {
response[key] = value
}
}
return response
}
func (o *publishPushHelperOpts) buildAPNSPayload() map[string]interface{} {
apns := make(map[string]interface{})
if o.PushAPNSData != nil {
aps := &o.PushAPNSData.APS
if aps != nil {
apsData := make(map[string]interface{})
if aps.Alert != nil {
apsData["alert"] = aps.Alert
} else if aps.Subtitle != "" || aps.Body != "" || aps.Title != "" {
alert := make(map[string]interface{})
if aps.Subtitle != "" {
alert["subtitle"] = aps.Subtitle
}
if aps.Title != "" {
alert["title"] = aps.Title
}
if aps.Body != "" {
alert["body"] = aps.Body
}
apsData["alert"] = alert
}
apsData["badge"] = aps.Badge
if aps.Sound != "" {
apsData["sound"] = aps.Sound
}
if aps.Custom != nil {
for key, value := range aps.Custom {
apsData[key] = value
}
}
apns["aps"] = apsData
}
custom := o.PushAPNSData.Custom
if custom != nil {
for key, value := range custom {
apns[key] = value
}
}
}
return apns
}
func (o *publishPushHelperOpts) buildMPNSPayload() map[string]interface{} {
mpns := make(map[string]interface{})
if o.PushMPNSData != nil {
if o.PushMPNSData.Title != "" {
mpns["title"] = o.PushMPNSData.Title
}
if o.PushMPNSData.Type != "" {
mpns["type"] = o.PushMPNSData.Type
}
if o.PushMPNSData.BackTitle != "" {
mpns["back_title"] = o.PushMPNSData.BackTitle
}
if o.PushMPNSData.BackContent != "" {
mpns["back_content"] = o.PushMPNSData.BackContent
}
mpns["count"] = o.PushMPNSData.Count
custom := o.PushMPNSData.Custom
if custom != nil {
for key, value := range custom {
mpns[key] = value
}
}
}
return mpns
}
func (o *publishPushHelperOpts) buildFCMPayload() map[string]interface{} {
fcm := make(map[string]interface{})
if o.PushFCMData != nil {
data := &o.PushFCMData.Data
if data != nil {
fcmData := make(map[string]interface{})
if data.Summary != nil {
fcmData["summary"] = data.Summary
}
custom := data.Custom
if custom != nil {
for key, value := range custom {
fcmData[key] = value
}
}
fcm["data"] = fcmData
}
custom := o.PushFCMData.Custom
if custom != nil {
for key, value := range custom {
fcm[key] = value
}
}
}
return fcm
}
// SetAPNSPayload sets the APNS payload
func (b *publishPushHelperBuilder) SetAPNSPayload(pnAPNSData PNAPNSData, pnAPNS2Data []PNAPNS2Data) *publishPushHelperBuilder {
b.opts.PushAPNSData = &pnAPNSData
b.opts.PushAPNS2Data = pnAPNS2Data
return b
}
// SetMPNSPayload sets the MPNS payload
func (b *publishPushHelperBuilder) SetMPNSPayload(pnMPNSData PNMPNSData) *publishPushHelperBuilder {
b.opts.PushMPNSData = &pnMPNSData
return b
}
// SetCommonPayload sets the common payload
func (b *publishPushHelperBuilder) SetCommonPayload(commonPayload map[string]interface{}) *publishPushHelperBuilder {
b.opts.CommonPayload = commonPayload
return b
}
// SetFCMPayload sets the FCM payload
func (b *publishPushHelperBuilder) SetFCMPayload(pnFCMData PNFCMData) *publishPushHelperBuilder {
b.opts.PushFCMData = &pnFCMData
return b
}
type publishPushHelperOpts struct {
pubnub *PubNub
PushAPNS2Data []PNAPNS2Data
PushAPNSData *PNAPNSData
PushMPNSData *PNMPNSData
PushFCMData *PNFCMData
CommonPayload map[string]interface{}
PushCustomData map[string]interface{}
ctx Context
}
func (o *publishPushHelperOpts) context() Context {
return o.ctx
}