|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
|
4
|
|
|
|
|
5
|
|
|
class WebPushMessage |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The notification id. |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $id = null; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The notification title. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $title; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The notification body. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $body; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The notification icon. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $icon = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The notification actions. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $actions = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $data = []; |
|
47
|
|
|
|
|
48
|
|
|
protected $image = null; |
|
49
|
|
|
protected $badge = null; |
|
50
|
|
|
protected $vibrate = [ ]; |
|
51
|
|
|
protected $timestamp = null; |
|
52
|
|
|
protected $tag = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $body |
|
56
|
|
|
* |
|
57
|
|
|
* @return static |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public static function create($body = '') |
|
60
|
|
|
{ |
|
61
|
3 |
|
return new static($body); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $body |
|
66
|
|
|
*/ |
|
67
|
9 |
|
public function __construct($body = '') |
|
68
|
|
|
{ |
|
69
|
9 |
|
$this->title = ''; |
|
70
|
|
|
|
|
71
|
9 |
|
$this->body = $body; |
|
72
|
9 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set the notification id. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $value |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function id($value) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->id = $value; |
|
83
|
|
|
|
|
84
|
3 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the notification title. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function title($value) |
|
94
|
|
|
{ |
|
95
|
3 |
|
$this->title = $value; |
|
96
|
|
|
|
|
97
|
3 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the notification body. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $value |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function body($value) |
|
107
|
|
|
{ |
|
108
|
3 |
|
$this->body = $value; |
|
109
|
|
|
|
|
110
|
3 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set the notification icon. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $value |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function icon($value) |
|
120
|
|
|
{ |
|
121
|
3 |
|
$this->icon = $value; |
|
122
|
|
|
|
|
123
|
3 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Set an action. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $title |
|
130
|
|
|
* @param string $action |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
3 |
|
public function action($title, $action, $icon = null, $placeholder = null, $type = "button") |
|
134
|
|
|
{ |
|
135
|
3 |
|
$this->actions[] = array_filter(compact('title', 'action', 'icon', 'placeholder', 'type')); |
|
136
|
|
|
|
|
137
|
3 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the arbitrary data payload. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $value |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function data($value) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->data = $value; |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set the badge. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $value |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function badge($value) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->badge = $value; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Set the image. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $value |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
|
|
public function image($value) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->image = $value; |
|
175
|
|
|
|
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Set the vibrate pattern. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $value |
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
|
|
public function vibrate($value) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->vibrate = $value; |
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Set the timestamp pattern. |
|
194
|
|
|
* |
|
195
|
|
|
* @param int $value |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function timestamp($value) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->timestamp = $value; |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Set the tag pattern. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $value |
|
209
|
|
|
* @return $this |
|
210
|
|
|
*/ |
|
211
|
|
|
public function tag($value) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->tag = $value; |
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get an array representation of the message. |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
9 |
|
public function toArray() |
|
224
|
|
|
{ |
|
225
|
|
|
$rtn = [ |
|
226
|
9 |
|
'id' => $this->id, |
|
227
|
9 |
|
'title' => $this->title, |
|
228
|
9 |
|
'body' => $this->body, |
|
229
|
9 |
|
'actions' => $this->actions, |
|
230
|
9 |
|
'icon' => $this->icon, |
|
231
|
9 |
|
'image' => $this->image, |
|
232
|
9 |
|
'badge' => $this->badge, |
|
233
|
9 |
|
'vibrate' => $this->vibrate, |
|
234
|
9 |
|
'timestamp' => $this->timestamp, |
|
235
|
9 |
|
'tag' => $this->tag, |
|
236
|
9 |
|
'data' => $this->data, |
|
237
|
9 |
|
]; |
|
238
|
|
|
// remove null values |
|
239
|
9 |
|
$rtn = array_filter($rtn); |
|
240
|
9 |
|
return $rtn; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|