1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pushmix\WebNotification; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Pushmix\WebNotification\Exceptions\MissingParameter; |
7
|
|
|
|
8
|
|
|
class PushmixMessage |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
public $to; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
public $title; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
public $body; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
public $default_url; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public $time_to_live; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public $priority; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public $icon; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $badge; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
public $image; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
public $buttons = []; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
public $extraParameters = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $body |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
15 |
|
public static function create($to) |
49
|
|
|
{ |
50
|
15 |
|
return new static($to); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $body |
55
|
|
|
*/ |
56
|
17 |
|
public function __construct($to = '') |
57
|
|
|
{ |
58
|
17 |
|
$this->to = ! empty($to) ? $to : 'all'; |
59
|
17 |
|
} |
60
|
|
|
|
61
|
|
|
/***/ |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set to parameter. |
65
|
|
|
* @param string $value specify subscribers topic, |
66
|
|
|
* 'all' -all subscribers, |
67
|
|
|
* 'one' - topic one subscribers, |
68
|
|
|
* 'two' - second topic subscribers |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
1 |
|
public function to($value) |
73
|
|
|
{ |
74
|
1 |
|
$this->to = $value; |
75
|
|
|
|
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/***/ |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set Notification Title. |
83
|
|
|
* @param string $value notification title |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
6 |
|
public function title($value) |
87
|
|
|
{ |
88
|
6 |
|
$this->title = $value; |
89
|
|
|
|
90
|
6 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/***/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the message body. |
97
|
|
|
* |
98
|
|
|
* @param string $value |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
5 |
|
public function body($value) |
103
|
|
|
{ |
104
|
5 |
|
$this->body = $value; |
105
|
|
|
|
106
|
5 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/***/ |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set Notification Default URL. |
113
|
|
|
* @param string $value notification default url |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
4 |
|
public function url($value) |
117
|
|
|
{ |
118
|
4 |
|
$this->default_url = $value; |
119
|
|
|
|
120
|
4 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/***/ |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set Time To Live Parameter. |
127
|
|
|
* @param string $value notification time to live value |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
2 |
|
public function ttl($value) |
131
|
|
|
{ |
132
|
2 |
|
$this->time_to_live = $value; |
133
|
|
|
|
134
|
2 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/***/ |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set Priority Parameter. |
141
|
|
|
* @param string $value notification priority value |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
2 |
|
public function priority($value) |
145
|
|
|
{ |
146
|
2 |
|
$this->priority = $value; |
147
|
|
|
|
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/***/ |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set Icon Parameter. |
155
|
|
|
* @param string $value notification icon value |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
2 |
|
public function icon($value) |
159
|
|
|
{ |
160
|
2 |
|
$this->icon = $value; |
161
|
|
|
|
162
|
2 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/***/ |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set Badge Parameter. |
169
|
|
|
* @param string $value notification badge value |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
2 |
|
public function badge($value) |
173
|
|
|
{ |
174
|
2 |
|
$this->badge = $value; |
175
|
|
|
|
176
|
2 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/***/ |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set Large Image Parameter. |
183
|
|
|
* @param string $value notification large image |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
2 |
|
public function image($value) |
187
|
|
|
{ |
188
|
2 |
|
$this->image = $value; |
189
|
|
|
|
190
|
2 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/***/ |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set Notification Buttons. |
197
|
|
|
* @param string $title button Title |
198
|
|
|
* @param string $ur button url |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
2 |
|
public function button($title, $url) |
202
|
|
|
{ |
203
|
2 |
|
$cnt = count($this->buttons); |
204
|
|
|
// supporting maximum two buttons |
205
|
2 |
|
if ($cnt >= 2) { |
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
switch ($cnt) { |
210
|
2 |
|
case 0: |
211
|
2 |
|
array_push($this->buttons, [ |
212
|
2 |
|
'action_title_one' => $title, |
213
|
2 |
|
'action_url_one' => $url, |
214
|
|
|
]); |
215
|
2 |
|
break; |
216
|
|
|
|
217
|
2 |
|
case 1: |
218
|
2 |
|
array_push($this->buttons, [ |
219
|
2 |
|
'action_title_two' => $title, |
220
|
2 |
|
'action_url_two' => $url, |
221
|
|
|
]); |
222
|
2 |
|
break; |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
2 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/***/ |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get Buttons. |
233
|
|
|
* @return array return an array of buttons |
234
|
|
|
*/ |
235
|
1 |
|
public function getButtons() |
236
|
|
|
{ |
237
|
1 |
|
return $this->buttons; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/***/ |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Create Array of Parameters. |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
7 |
|
public function toArray() |
247
|
|
|
{ |
248
|
7 |
|
if (empty($this->to)) { |
249
|
1 |
|
throw MissingParameter::error('to'); |
250
|
|
|
} |
251
|
|
|
|
252
|
6 |
|
if (empty($this->title)) { |
253
|
1 |
|
throw MissingParameter::error('title'); |
254
|
|
|
} |
255
|
|
|
|
256
|
5 |
|
if (empty($this->body)) { |
257
|
1 |
|
throw MissingParameter::error('body'); |
258
|
|
|
} |
259
|
|
|
|
260
|
4 |
|
if (empty($this->default_url)) { |
261
|
1 |
|
throw MissingParameter::error('url'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$message = [ |
265
|
3 |
|
'to' => $this->to, |
266
|
3 |
|
'title' => $this->title, |
267
|
3 |
|
'body' => $this->body, |
268
|
3 |
|
'default_url' => $this->default_url, |
269
|
|
|
]; |
270
|
|
|
|
271
|
|
|
/* |
272
|
|
|
* Set Optional Parameters |
273
|
|
|
* @var [type] |
274
|
|
|
*/ |
275
|
3 |
|
if (! empty($this->priority)) { |
276
|
1 |
|
$message['priority'] = $this->priority; |
277
|
|
|
} |
278
|
|
|
|
279
|
3 |
|
if (! empty($this->time_to_live)) { |
280
|
1 |
|
$message['time_to_live'] = $this->time_to_live; |
281
|
|
|
} |
282
|
|
|
|
283
|
3 |
|
if (! empty($this->icon)) { |
284
|
1 |
|
$message['icon'] = $this->icon; |
285
|
|
|
} |
286
|
|
|
|
287
|
3 |
|
if (! empty($this->badge)) { |
288
|
1 |
|
$message['badge'] = $this->badge; |
289
|
|
|
} |
290
|
|
|
|
291
|
3 |
|
if (! empty($this->image)) { |
292
|
1 |
|
$message['image'] = $this->image; |
293
|
|
|
} |
294
|
|
|
|
295
|
3 |
|
foreach ($this->buttons as $data => $value) { |
296
|
1 |
|
foreach ($value as $key => $val) { |
297
|
1 |
|
Arr::set($message, $key, $val); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
3 |
|
return $message; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/***/ |
305
|
|
|
} |
306
|
|
|
|