1 | <?php |
||
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() |
||
160 | |||
161 | /** |
||
162 | * Set badge. |
||
163 | * |
||
164 | * @param int $value |
||
165 | * |
||
166 | * @return Payload |
||
167 | */ |
||
168 | public function setBadge(int $value): Payload |
||
174 | |||
175 | /** |
||
176 | * Get sound. |
||
177 | * |
||
178 | * @return string|null |
||
179 | */ |
||
180 | public function getSound() |
||
184 | |||
185 | /** |
||
186 | * Set sound. |
||
187 | * |
||
188 | * @param string $value |
||
189 | * |
||
190 | * @return Payload |
||
191 | */ |
||
192 | public function setSound(string $value): Payload |
||
198 | |||
199 | /** |
||
200 | * Set content availability. |
||
201 | * |
||
202 | * @param bool $value |
||
203 | * |
||
204 | * @return Payload |
||
205 | */ |
||
206 | public function setContentAvailability(bool $value): Payload |
||
212 | |||
213 | /** |
||
214 | * Get content availability. |
||
215 | * |
||
216 | * @return bool|null |
||
217 | */ |
||
218 | public function isContentAvailable() |
||
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 |
||
238 | |||
239 | /** |
||
240 | * Is content mutable. |
||
241 | * |
||
242 | * @return bool|null |
||
243 | */ |
||
244 | public function hasMutableContent() |
||
248 | |||
249 | /** |
||
250 | * Get category. |
||
251 | * |
||
252 | * @return string|null |
||
253 | */ |
||
254 | public function getCategory() |
||
258 | |||
259 | /** |
||
260 | * Set category. |
||
261 | * |
||
262 | * @param string $value |
||
263 | * |
||
264 | * @return Payload |
||
265 | */ |
||
266 | public function setCategory(string $value): Payload |
||
272 | |||
273 | /** |
||
274 | * Get thread-id. |
||
275 | * |
||
276 | * @return string|null |
||
277 | */ |
||
278 | public function getThreadId() |
||
282 | |||
283 | /** |
||
284 | * Set thread-id. |
||
285 | * |
||
286 | * @param string $value |
||
287 | * |
||
288 | * @return Payload |
||
289 | */ |
||
290 | public function setThreadId(string $value): Payload |
||
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 |
||
316 | |||
317 | /** |
||
318 | * Get custom value. |
||
319 | * |
||
320 | * @param $key |
||
321 | * |
||
322 | * @return mixed |
||
323 | * @throws InvalidPayloadException |
||
324 | */ |
||
325 | public function getCustomValue($key) |
||
333 | |||
334 | /** |
||
335 | * Set push type for Payload. |
||
336 | * |
||
337 | * @param string $pushType |
||
338 | * @return Payload |
||
339 | */ |
||
340 | public function setPushType($pushType) { |
||
345 | |||
346 | /** |
||
347 | * Get push type for Payload. |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | public function getPushType() { |
||
354 | |||
355 | /** |
||
356 | * Convert Payload to JSON. |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | public function toJson(): string |
||
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() |
||
409 | |||
410 | /** |
||
411 | * Get default payload structure. |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | private static function getDefaultPayloadStructure() |
||
419 | } |
||
420 |