Skip to content

Commit 8bdc74a

Browse files
committed
Make payload public and format docs
1 parent 747a85b commit 8bdc74a

File tree

1 file changed

+82
-61
lines changed

1 file changed

+82
-61
lines changed

payload/builder.go

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ package payload
44

55
import "encoding/json"
66

7-
type payload struct {
7+
// Payload represents a notification which holds the content that will be
8+
// marshalled as JSON.
9+
type Payload struct {
810
content map[string]interface{}
911
}
1012

@@ -29,189 +31,208 @@ type alert struct {
2931
TitleLocKey string `json:"title-loc-key,omitempty"`
3032
}
3133

32-
// NewPayload represents a notification payload.
33-
func NewPayload() *payload {
34-
return &payload{
34+
// NewPayload returns a new Payload struct
35+
func NewPayload() *Payload {
36+
return &Payload{
3537
map[string]interface{}{
3638
"aps": &aps{},
3739
},
3840
}
3941
}
4042

41-
// Sets the aps alert on the payload.
42-
// This will display a notification alert message to the user.
43-
// {"aps":{"alert":alert}}
44-
func (p *payload) Alert(alert interface{}) *payload {
43+
// Alert sets the aps alert on the payload.
44+
// This will display a notification alert message to the <b>user.</b>
45+
//
46+
// {"aps":{"alert":alert}}`
47+
func (p *Payload) Alert(alert interface{}) *Payload {
4548
p.aps().Alert = alert
4649
return p
4750
}
4851

49-
// Sets the aps badge on the payload.
52+
// Badge sets the aps badge on the payload.
5053
// This will display a numeric badge on the app icon.
51-
// {"aps":{"badge":b}}
52-
func (p *payload) Badge(b int) *payload {
54+
//
55+
// {"aps":{"badge":b}}
56+
func (p *Payload) Badge(b int) *Payload {
5357
p.aps().Badge = b
5458
return p
5559
}
5660

57-
// Sets the aps badge on the payload to 0.
61+
// ZeroBadge sets the aps badge on the payload to 0.
5862
// This will clear the badge on the app icon.
59-
// {"aps":{"badge":0}}
60-
func (p *payload) ZeroBadge() *payload {
63+
//
64+
// {"aps":{"badge":0}}
65+
func (p *Payload) ZeroBadge() *Payload {
6166
p.aps().Badge = 0
6267
return p
6368
}
6469

65-
// Removes the badge attribute from the payload.
70+
// UnsetBadge removes the badge attribute from the payload.
6671
// This will leave the badge on the app icon unchanged.
6772
// If you wish to clear the app icon badge, use ZeroBadge() instead.
68-
// {"aps":{}}
69-
func (p *payload) UnsetBadge() *payload {
73+
//
74+
// {"aps":{}}
75+
func (p *Payload) UnsetBadge() *Payload {
7076
p.aps().Badge = nil
7177
return p
7278
}
7379

74-
// Sets the aps sound on the payload.
80+
// Sound sets the aps sound on the payload.
7581
// This will play a sound from the app bundle, or the default sound otherwise.
76-
// {"aps":{"sound":sound}}
77-
func (p *payload) Sound(sound string) *payload {
82+
//
83+
// {"aps":{"sound":sound}}
84+
func (p *Payload) Sound(sound string) *Payload {
7885
p.aps().Sound = sound
7986
return p
8087
}
8188

82-
// Sets the aps content-available on the payload to 1.
89+
// ContentAvailable sets the aps content-available on the payload to 1.
8390
// This will indicate to the app that there is new content available to download
8491
// and launch the app in the background.
85-
// {"aps":{"content-available":1}}
86-
func (p *payload) ContentAvailable() *payload {
92+
//
93+
// {"aps":{"content-available":1}}
94+
func (p *Payload) ContentAvailable() *Payload {
8795
p.aps().ContentAvailable = 1
8896
return p
8997
}
9098

9199
// Custom payload
92100

93-
// Sets a custom key and value on the payload.
101+
// Custom sets a custom key and value on the payload.
94102
// This will add custom key/value data to the notification payload at root level.
95-
// {"aps":{}, key:value}
96-
func (p *payload) Custom(key string, val interface{}) *payload {
103+
//
104+
// {"aps":{}, key:value}
105+
func (p *Payload) Custom(key string, val interface{}) *Payload {
97106
p.content[key] = val
98107
return p
99108
}
100109

101110
// Alert dictionary
102111

103-
// Sets the aps alert title on the payload.
112+
// AlertTitle sets the aps alert title on the payload.
104113
// This will display a short string describing the purpose of the notification.
105114
// Apple Watch & Safari display this string as part of the notification interface.
106-
// {"aps":{"alert":"title"}}
107-
func (p *payload) AlertTitle(title string) *payload {
115+
//
116+
// {"aps":{"alert":"title"}}
117+
func (p *Payload) AlertTitle(title string) *Payload {
108118
p.aps().alert().Title = title
109119
return p
110120
}
111121

112-
// Sets the aps alert title localization key on the payload.
122+
// AlertTitleLocKey sets the aps alert title localization key on the payload.
113123
// This is the key to a title string in the Localizable.strings file for the
114124
// current localization. See Localized Formatted Strings in Apple documentation
115125
// for more information.
116-
// {"aps":{"alert":{"title-loc-key":key}}}
117-
func (p *payload) AlertTitleLocKey(key string) *payload {
126+
//
127+
// {"aps":{"alert":{"title-loc-key":key}}}
128+
func (p *Payload) AlertTitleLocKey(key string) *Payload {
118129
p.aps().alert().TitleLocKey = key
119130
return p
120131
}
121132

122-
// Sets the aps alert title localization args on the payload.
133+
// AlertTitleLocArgs sets the aps alert title localization args on the payload.
123134
// These are the variable string values to appear in place of the format
124135
// specifiers in title-loc-key. See Localized Formatted Strings in Apple
125136
// documentation for more information.
126-
// {"aps":{"alert":{"title-loc-args":args}}}
127-
func (p *payload) AlertTitleLocArgs(args []string) *payload {
137+
//
138+
// {"aps":{"alert":{"title-loc-args":args}}}
139+
func (p *Payload) AlertTitleLocArgs(args []string) *Payload {
128140
p.aps().alert().TitleLocArgs = args
129141
return p
130142
}
131143

132-
// Sets the aps alert body on the payload.
144+
// AlertBody sets the aps alert body on the payload.
133145
// This is the text of the alert message.
134-
// {"aps":{"alert":{"body":body}}}
135-
func (p *payload) AlertBody(body string) *payload {
146+
//
147+
// {"aps":{"alert":{"body":body}}}
148+
func (p *Payload) AlertBody(body string) *Payload {
136149
p.aps().alert().Body = body
137150
return p
138151
}
139152

140-
// Sets the aps launch image on the payload.
153+
// AlertLaunchImage sets the aps launch image on the payload.
141154
// This is the filename of an image file in the app bundle. The image is used
142155
// as the launch image when users tap the action button or move the action
143156
// slider.
144-
// {"aps":{"alert":{"launch-image":image}}}
145-
func (p *payload) AlertLaunchImage(image string) *payload {
157+
//
158+
// {"aps":{"alert":{"launch-image":image}}}
159+
func (p *Payload) AlertLaunchImage(image string) *Payload {
146160
p.aps().alert().LaunchImage = image
147161
return p
148162
}
149163

150-
// Sets the aps alert localization key on the payload.
164+
// AlertLocKey sets the aps alert localization key on the payload.
151165
// This is the key to an alert-message string in the Localizable.strings file
152166
// for the current localization. See Localized Formatted Strings in Apple
153167
// documentation for more information.
154-
// {"aps":{"alert":{"loc-key":key}}}
155-
func (p *payload) AlertLocKey(key string) *payload {
168+
//
169+
// {"aps":{"alert":{"loc-key":key}}}
170+
func (p *Payload) AlertLocKey(key string) *Payload {
156171
p.aps().alert().LocKey = key
157172
return p
158173
}
159174

160-
// Sets the aps alert action on the payload.
175+
// AlertAction sets the aps alert action on the payload.
161176
// This is the label of the action button, if the user sets the notifications
162177
// to appear as alerts. This label should be succinct, such as “Details” or
163178
// “Read more”. If omitted, the default value is “Show”.
164-
// {"aps":{"alert":{"action":action}}}
165-
func (p *payload) AlertAction(action string) *payload {
179+
//
180+
// {"aps":{"alert":{"action":action}}}
181+
func (p *Payload) AlertAction(action string) *Payload {
166182
p.aps().alert().Action = action
167183
return p
168184
}
169185

170-
// Sets the aps alert action localization key on the payload.
186+
// AlertActionLocKey sets the aps alert action localization key on the payload.
171187
// This is the the string used as a key to get a localized string in the current
172188
// localization to use for the notfication right button’s title instead of
173189
// “View”. See Localized Formatted Strings in Apple documentation for more
174190
// information.
175-
// {"aps":{"alert":{"action-loc-key":key}}}
176-
func (p *payload) AlertActionLocKey(key string) *payload {
191+
//
192+
// {"aps":{"alert":{"action-loc-key":key}}}
193+
func (p *Payload) AlertActionLocKey(key string) *Payload {
177194
p.aps().alert().ActionLocKey = key
178195
return p
179196
}
180197

181198
// General
182199

183-
// Sets the aps category on the payload.
200+
// Category sets the aps category on the payload.
184201
// This is a string value that represents the identifier property of the
185202
// UIMutableUserNotificationCategory object you created to define custom actions.
186-
// {"aps":{"alert":{"category":category}}}
187-
func (p *payload) Category(category string) *payload {
203+
//
204+
// {"aps":{"alert":{"category":category}}}
205+
func (p *Payload) Category(category string) *Payload {
188206
p.aps().Category = category
189207
return p
190208
}
191209

192-
// Sets the mdm on the payload.
210+
// Mdm sets the mdm on the payload.
193211
// This is for Apple Mobile Device Management (mdm) payloads.
194-
// {"aps":{}:"mdm":mdm}
195-
func (p *payload) Mdm(mdm string) *payload {
212+
//
213+
// {"aps":{}:"mdm":mdm}
214+
func (p *Payload) Mdm(mdm string) *Payload {
196215
p.content["mdm"] = mdm
197216
return p
198217
}
199218

200-
// Sets the aps category on the payload.
219+
// URLArgs sets the aps category on the payload.
201220
// This specifies an array of values that are paired with the placeholders
202221
// inside the urlFormatString value of your website.json file.
203222
// See Apple Notification Programming Guide for Websites.
204-
// {"aps":{"url-args":urlArgs}}
205-
func (p *payload) URLArgs(urlArgs []string) *payload {
223+
//
224+
// {"aps":{"url-args":urlArgs}}
225+
func (p *Payload) URLArgs(urlArgs []string) *Payload {
206226
p.aps().URLArgs = urlArgs
207227
return p
208228
}
209229

210-
func (p *payload) MarshalJSON() ([]byte, error) {
230+
// MarshalJSON returns the JSON encoded version of the Payload
231+
func (p *Payload) MarshalJSON() ([]byte, error) {
211232
return json.Marshal(p.content)
212233
}
213234

214-
func (p *payload) aps() *aps {
235+
func (p *Payload) aps() *aps {
215236
return p.content["aps"].(*aps)
216237
}
217238

0 commit comments

Comments
 (0)