1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pushok package. |
5
|
|
|
* |
6
|
|
|
* (c) Arthur Edamov <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pushok; |
13
|
|
|
|
14
|
|
|
use Countable; |
15
|
|
|
use Pushok\Payload\Alert; |
16
|
|
|
|
17
|
|
|
// Polyfill for PHP 7.2 |
18
|
|
|
if (!function_exists('is_countable')) { |
19
|
|
|
function is_countable($var) |
20
|
|
|
{ |
21
|
|
|
return (is_array($var) || $var instanceof Countable); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Payload |
27
|
|
|
* |
28
|
|
|
* @package Pushok |
29
|
|
|
* |
30
|
|
|
* @see http://bit.ly/payload-key-reference |
31
|
|
|
*/ |
32
|
|
|
class Payload implements \JsonSerializable |
33
|
|
|
{ |
34
|
|
|
const PAYLOAD_ROOT_KEY = 'aps'; |
35
|
|
|
const PAYLOAD_ALERT_KEY = 'alert'; |
36
|
|
|
const PAYLOAD_BADGE_KEY = 'badge'; |
37
|
|
|
const PAYLOAD_SOUND_KEY = 'sound'; |
38
|
|
|
const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available'; |
39
|
|
|
const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content'; |
40
|
|
|
const PAYLOAD_CATEGORY_KEY = 'category'; |
41
|
|
|
const PAYLOAD_THREAD_ID_KEY = 'thread-id'; |
42
|
|
|
|
43
|
|
|
const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096; |
44
|
|
|
const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120; |
45
|
|
|
const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The notification settings for your app on the user’s device determine whether an alert or banner is displayed. |
49
|
|
|
* |
50
|
|
|
* @var Alert|string |
51
|
|
|
*/ |
52
|
|
|
private $alert; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The number to display as the badge of the app icon. |
56
|
|
|
* If this property is absent, the badge is not changed. |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
private $badge; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $sound; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Include this key with a value of true to configure a silent notification. |
71
|
|
|
* |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
private $contentAvailable; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Include this key with a value of true to configure a mutable content notification. |
78
|
|
|
* |
79
|
|
|
* @var bool |
80
|
|
|
*/ |
81
|
|
|
private $mutableContent; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Provide this key with a string value that represents the notification’s type. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private $category; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Provide this key with a string value that represents the app-specific identifier for grouping notifications. |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
private $threadId; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Payload custom values. |
99
|
|
|
* |
100
|
|
|
* @var array |
101
|
|
|
*/ |
102
|
|
|
private $customValues; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Push notification type |
106
|
|
|
* |
107
|
|
|
* https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns#2947607 |
108
|
|
|
* |
109
|
|
|
* @var string |
110
|
|
|
*/ |
111
|
|
|
private $pushType; |
112
|
|
|
|
113
|
|
|
protected function __construct() |
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Payload |
119
|
|
|
*/ |
120
|
|
|
public static function create(): Payload |
121
|
|
|
{ |
122
|
|
|
return new self(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get Alert. |
127
|
|
|
* |
128
|
|
|
* @return Alert|null |
129
|
|
|
*/ |
130
|
|
|
public function getAlert() |
131
|
|
|
{ |
132
|
|
|
return $this->alert; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set Alert. |
137
|
|
|
* |
138
|
|
|
* @param Alert|string $alert |
139
|
|
|
* |
140
|
|
|
* @return Payload |
141
|
|
|
*/ |
142
|
|
|
public function setAlert($alert): Payload |
143
|
|
|
{ |
144
|
|
|
if ($alert instanceof Alert || is_string($alert)) { |
145
|
|
|
$this->alert = $alert; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get badge. |
153
|
|
|
* |
154
|
|
|
* @return int|null |
155
|
|
|
*/ |
156
|
|
|
public function getBadge() |
157
|
|
|
{ |
158
|
|
|
return $this->badge; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set badge. |
163
|
|
|
* |
164
|
|
|
* @param int $value |
165
|
|
|
* |
166
|
|
|
* @return Payload |
167
|
|
|
*/ |
168
|
|
|
public function setBadge(int $value): Payload |
169
|
|
|
{ |
170
|
|
|
$this->badge = $value; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get sound. |
177
|
|
|
* |
178
|
|
|
* @return string|null |
179
|
|
|
*/ |
180
|
|
|
public function getSound() |
181
|
|
|
{ |
182
|
|
|
return $this->sound; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set sound. |
187
|
|
|
* |
188
|
|
|
* @param string $value |
189
|
|
|
* |
190
|
|
|
* @return Payload |
191
|
|
|
*/ |
192
|
|
|
public function setSound(string $value): Payload |
193
|
|
|
{ |
194
|
|
|
$this->sound = $value; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set content availability. |
201
|
|
|
* |
202
|
|
|
* @param bool $value |
203
|
|
|
* |
204
|
|
|
* @return Payload |
205
|
|
|
*/ |
206
|
|
|
public function setContentAvailability(bool $value): Payload |
207
|
|
|
{ |
208
|
|
|
$this->contentAvailable = $value; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get content availability. |
215
|
|
|
* |
216
|
|
|
* @return bool|null |
217
|
|
|
*/ |
218
|
|
|
public function isContentAvailable() |
219
|
|
|
{ |
220
|
|
|
return $this->contentAvailable; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the mutable-content key for Notification Service Extensions on iOS10. |
225
|
|
|
* |
226
|
|
|
* @see http://bit.ly/mutable-content |
227
|
|
|
* |
228
|
|
|
* @param bool $value |
229
|
|
|
* |
230
|
|
|
* @return Payload |
231
|
|
|
*/ |
232
|
|
|
public function setMutableContent(bool $value): Payload |
233
|
|
|
{ |
234
|
|
|
$this->mutableContent = $value; |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Is content mutable. |
241
|
|
|
* |
242
|
|
|
* @return bool|null |
243
|
|
|
*/ |
244
|
|
|
public function hasMutableContent() |
245
|
|
|
{ |
246
|
|
|
return $this->mutableContent; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get category. |
251
|
|
|
* |
252
|
|
|
* @return string|null |
253
|
|
|
*/ |
254
|
|
|
public function getCategory() |
255
|
|
|
{ |
256
|
|
|
return $this->category; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set category. |
261
|
|
|
* |
262
|
|
|
* @param string $value |
263
|
|
|
* |
264
|
|
|
* @return Payload |
265
|
|
|
*/ |
266
|
|
|
public function setCategory(string $value): Payload |
267
|
|
|
{ |
268
|
|
|
$this->category = $value; |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get thread-id. |
275
|
|
|
* |
276
|
|
|
* @return string|null |
277
|
|
|
*/ |
278
|
|
|
public function getThreadId() |
279
|
|
|
{ |
280
|
|
|
return $this->threadId; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Set thread-id. |
285
|
|
|
* |
286
|
|
|
* @param string $value |
287
|
|
|
* |
288
|
|
|
* @return Payload |
289
|
|
|
*/ |
290
|
|
|
public function setThreadId(string $value): Payload |
291
|
|
|
{ |
292
|
|
|
$this->threadId = $value; |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Set custom value for Payload. |
299
|
|
|
* |
300
|
|
|
* @param string $key |
301
|
|
|
* @param mixed $value |
302
|
|
|
* |
303
|
|
|
* @return Payload |
304
|
|
|
* @throws InvalidPayloadException |
305
|
|
|
*/ |
306
|
|
|
public function setCustomValue(string $key, $value): Payload |
307
|
|
|
{ |
308
|
|
|
if ($key === self::PAYLOAD_ROOT_KEY) { |
309
|
|
|
throw InvalidPayloadException::reservedKey(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->customValues[$key] = $value; |
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get custom value. |
319
|
|
|
* |
320
|
|
|
* @param $key |
321
|
|
|
* |
322
|
|
|
* @return mixed |
323
|
|
|
* @throws InvalidPayloadException |
324
|
|
|
*/ |
325
|
|
|
public function getCustomValue($key) |
326
|
|
|
{ |
327
|
|
|
if (!array_key_exists($key, $this->customValues)) { |
328
|
|
|
throw InvalidPayloadException::notExistingCustomValue($key); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return $this->customValues[$key]; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set push type for Payload. |
336
|
|
|
* |
337
|
|
|
* @param string $pushType |
338
|
|
|
* @return Payload |
339
|
|
|
*/ |
340
|
|
|
public function setPushType($pushType) { |
341
|
|
|
$this->pushType = $pushType; |
342
|
|
|
|
343
|
|
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Get push type for Payload. |
348
|
|
|
* |
349
|
|
|
* @return string |
350
|
|
|
*/ |
351
|
|
|
public function getPushType() { |
352
|
|
|
return $this->pushType; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Convert Payload to JSON. |
357
|
|
|
* |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public function toJson(): string |
361
|
|
|
{ |
362
|
|
|
return json_encode($this, JSON_UNESCAPED_UNICODE); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Specify data which should be serialized to JSON. |
367
|
|
|
* |
368
|
|
|
* @return array |
369
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
370
|
|
|
*/ |
371
|
|
|
public function jsonSerialize() |
372
|
|
|
{ |
373
|
|
|
$payload = self::getDefaultPayloadStructure(); |
374
|
|
|
|
375
|
|
|
if ($this->alert instanceof Alert || is_string($this->alert)) { |
376
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if (is_int($this->badge)) { |
380
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_BADGE_KEY} = $this->badge; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if (is_string($this->sound)) { |
384
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_SOUND_KEY} = $this->sound; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
if (is_bool($this->contentAvailable)) { |
388
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CONTENT_AVAILABLE_KEY} = (int)$this->contentAvailable; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (is_bool($this->mutableContent)) { |
392
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_MUTABLE_CONTENT_KEY} = (int)$this->mutableContent; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
if (is_string($this->category)) { |
396
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CATEGORY_KEY} = $this->category; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
if (is_string($this->threadId)) { |
400
|
|
|
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (is_countable($this->customValues) && count($this->customValues)) { |
404
|
|
|
$payload = array_merge($payload, $this->customValues); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return $payload; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Get default payload structure. |
412
|
|
|
* |
413
|
|
|
* @return array |
414
|
|
|
*/ |
415
|
|
|
private static function getDefaultPayloadStructure() |
416
|
|
|
{ |
417
|
|
|
return [self::PAYLOAD_ROOT_KEY => new \stdClass]; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|