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