1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#Parameters |
7
|
|
|
*/ |
8
|
|
|
class WebPushMessage |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $title; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $actions = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $badge; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $body; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $dir; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $icon; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $image; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $lang; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
protected $renotify; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
protected $requireInteraction; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $tag; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $vibrate; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var mixed |
72
|
|
|
*/ |
73
|
|
|
protected $data; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the notification title. |
77
|
|
|
* |
78
|
|
|
* @param string $value |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
3 |
|
public function title($value) |
82
|
|
|
{ |
83
|
3 |
|
$this->title = $value; |
84
|
|
|
|
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a notification action. |
90
|
|
|
* |
91
|
|
|
* @param string $title |
92
|
|
|
* @param string $action |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
3 |
|
public function action($title, $action) |
96
|
|
|
{ |
97
|
3 |
|
$this->actions[] = compact('title', 'action'); |
98
|
|
|
|
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the notification badge. |
104
|
|
|
* |
105
|
|
|
* @param string $value |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
1 |
|
public function badge($value) |
109
|
|
|
{ |
110
|
1 |
|
$this->badge = $value; |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the notification body. |
117
|
|
|
* |
118
|
|
|
* @param string $value |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
3 |
|
public function body($value) |
122
|
|
|
{ |
123
|
3 |
|
$this->body = $value; |
124
|
|
|
|
125
|
3 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the notification direction. |
130
|
|
|
* |
131
|
|
|
* @param string $value |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
1 |
|
public function dir($value) |
135
|
|
|
{ |
136
|
1 |
|
$this->dir = $value; |
137
|
|
|
|
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the notification icon url. |
143
|
|
|
* |
144
|
|
|
* @param string $value |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
3 |
|
public function icon($value) |
148
|
|
|
{ |
149
|
3 |
|
$this->icon = $value; |
150
|
|
|
|
151
|
3 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the notification image url. |
156
|
|
|
* |
157
|
|
|
* @param string $value |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
1 |
|
public function image($value) |
161
|
|
|
{ |
162
|
1 |
|
$this->image = $value; |
163
|
|
|
|
164
|
1 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the notification language. |
169
|
|
|
* |
170
|
|
|
* @param string $value |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
1 |
|
public function lang($value) |
174
|
|
|
{ |
175
|
1 |
|
$this->lang = $value; |
176
|
|
|
|
177
|
1 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param bool $value |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
1 |
|
public function renotify($value = true) |
185
|
|
|
{ |
186
|
1 |
|
$this->renotify = $value; |
187
|
|
|
|
188
|
1 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param bool $value |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
1 |
|
public function requireInteraction($value = true) |
196
|
|
|
{ |
197
|
1 |
|
$this->requireInteraction = $value; |
198
|
|
|
|
199
|
1 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set the notification tag. |
204
|
|
|
* |
205
|
|
|
* @param string $value |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
1 |
|
public function tag($value) |
209
|
|
|
{ |
210
|
1 |
|
$this->tag = $value; |
211
|
|
|
|
212
|
1 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set the notification vibration pattern. |
217
|
|
|
* |
218
|
|
|
* @param array $value |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
1 |
|
public function vibrate($value) |
222
|
|
|
{ |
223
|
1 |
|
$this->vibrate = $value; |
224
|
|
|
|
225
|
1 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set the notification arbitrary data. |
230
|
|
|
* |
231
|
|
|
* @param mixed $value |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
3 |
|
public function data($value) |
235
|
|
|
{ |
236
|
3 |
|
$this->data = $value; |
237
|
|
|
|
238
|
3 |
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get an array representation of the message. |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
15 |
|
public function toArray() |
247
|
|
|
{ |
248
|
15 |
|
return collect([ |
249
|
15 |
|
'title', |
250
|
|
|
'actions', |
251
|
|
|
'badge', |
252
|
|
|
'body', |
253
|
|
|
'dir', |
254
|
|
|
'icon', |
255
|
|
|
'image', |
256
|
|
|
'lang', |
257
|
|
|
'renotify', |
258
|
|
|
'requireInteraction', |
259
|
|
|
'tag', |
260
|
|
|
'vibrate', |
261
|
|
|
'data', |
262
|
|
|
]) |
263
|
|
|
->mapWithKeys(function ($option) { |
264
|
15 |
|
return [$option => $this->{$option}]; |
265
|
15 |
|
}) |
266
|
15 |
|
->reject(function ($value) { |
267
|
15 |
|
return is_null($value); |
268
|
15 |
|
}) |
269
|
15 |
|
->toArray(); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|