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 Pushok\Payload\Alert; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Payload |
18
|
|
|
* @package Pushok |
19
|
|
|
* |
20
|
|
|
* @see http://bit.ly/payload-key-reference |
21
|
|
|
*/ |
22
|
|
|
class Payload |
23
|
|
|
{ |
24
|
|
|
const PAYLOAD_ROOT_KEY = 'aps'; |
25
|
|
|
const PAYLOAD_ALERT_KEY = 'alert'; |
26
|
|
|
const PAYLOAD_BADGE_KEY = 'badge'; |
27
|
|
|
const PAYLOAD_SOUND_KEY = 'sound'; |
28
|
|
|
const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available'; |
29
|
|
|
const PAYLOAD_CATEGORY_KEY = 'category'; |
30
|
|
|
const PAYLOAD_THREAD_ID_KEY = 'thread-id'; |
31
|
|
|
|
32
|
|
|
const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096; |
33
|
|
|
const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120; |
34
|
|
|
const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The notification settings for your app on the user’s device determine whether an alert or banner is displayed. |
38
|
|
|
* |
39
|
|
|
* @var Alert |
40
|
|
|
*/ |
41
|
|
|
private $alert; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The number to display as the badge of the app icon. |
45
|
|
|
* If this property is absent, the badge is not changed. |
46
|
|
|
* |
47
|
|
|
* @var integer |
48
|
|
|
*/ |
49
|
|
|
private $badge; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $sound; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Include this key with a value of true to configure a silent notification. |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
private $contentAvailable; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Provide this key with a string value that represents the notification’s type. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private $category; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Provide this key with a string value that represents the app-specific identifier for grouping notifications. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
private $threadId; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Payload custom values. |
81
|
|
|
* |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private $customValues; |
85
|
|
|
|
86
|
|
|
protected function __constructor() |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Payload |
92
|
|
|
*/ |
93
|
|
|
public static function create(): Payload |
94
|
|
|
{ |
95
|
|
|
return new self(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set alert. |
100
|
|
|
* |
101
|
|
|
* @param Alert $alert |
102
|
|
|
* @return Payload |
103
|
|
|
*/ |
104
|
|
|
public function setAlert(Alert $alert): Payload |
105
|
|
|
{ |
106
|
|
|
$this->alert = $alert; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get Alert. |
113
|
|
|
* |
114
|
|
|
* @return Alert|null |
115
|
|
|
*/ |
116
|
|
|
public function getAlert() |
117
|
|
|
{ |
118
|
|
|
return $this->alert; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set badge. |
123
|
|
|
* |
124
|
|
|
* @param integer $value |
125
|
|
|
* @return Payload |
126
|
|
|
*/ |
127
|
|
|
public function setBadge(integer $value): Payload |
128
|
|
|
{ |
129
|
|
|
$this->badge = $value; |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get badge. |
136
|
|
|
* |
137
|
|
|
* @return int|null |
138
|
|
|
*/ |
139
|
|
|
public function getBadge() |
140
|
|
|
{ |
141
|
|
|
return $this->badge; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set sound. |
146
|
|
|
* |
147
|
|
|
* @param string $value |
148
|
|
|
* @return Payload |
149
|
|
|
*/ |
150
|
|
|
public function setSound(string $value): Payload |
151
|
|
|
{ |
152
|
|
|
$this->sound = $value; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get sound. |
159
|
|
|
* |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
public function getSound() |
163
|
|
|
{ |
164
|
|
|
return $this->sound; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set content availability. |
169
|
|
|
* |
170
|
|
|
* @param bool $value |
171
|
|
|
* @return Payload |
172
|
|
|
*/ |
173
|
|
|
public function isContentAvailable(bool $value): Payload |
174
|
|
|
{ |
175
|
|
|
$this->contentAvailable = $value; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set category. |
182
|
|
|
* |
183
|
|
|
* @param string $value |
184
|
|
|
* @return Payload |
185
|
|
|
*/ |
186
|
|
|
public function setCategory(string $value): Payload |
187
|
|
|
{ |
188
|
|
|
$this->threadId = $value; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set thread-id. |
195
|
|
|
* |
196
|
|
|
* @param string $value |
197
|
|
|
* @return Payload |
198
|
|
|
*/ |
199
|
|
|
public function setThreadId(string $value): Payload |
200
|
|
|
{ |
201
|
|
|
$this->threadId = $value; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set custom value for Payload. |
208
|
|
|
* |
209
|
|
|
* @param string $key |
210
|
|
|
* @param $value |
211
|
|
|
* @return Payload |
212
|
|
|
* @throws InvalidPayloadException |
213
|
|
|
*/ |
214
|
|
|
public function setCustomValue(string $key, $value): Payload |
215
|
|
|
{ |
216
|
|
|
if ($key === self::PAYLOAD_ROOT_KEY) { |
217
|
|
|
throw InvalidPayloadException::reservedKey(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->customValues[$key] = $value; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Convert Payload to JSON. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function toJson(): string |
231
|
|
|
{ |
232
|
|
|
$payload = $this->transform(); |
233
|
|
|
|
234
|
|
|
return json_encode($payload); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Transform Payload object to array. |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
private function transform(): array |
243
|
|
|
{ |
244
|
|
|
$payload = [ |
245
|
|
|
self::PAYLOAD_ROOT_KEY => [ |
246
|
|
|
self::PAYLOAD_ALERT_KEY => $this->alert, |
247
|
|
|
self::PAYLOAD_BADGE_KEY => $this->badge, |
248
|
|
|
self::PAYLOAD_SOUND_KEY => $this->sound, |
249
|
|
|
self::PAYLOAD_CONTENT_AVAILABLE_KEY => $this->contentAvailable, |
250
|
|
|
self::PAYLOAD_CATEGORY_KEY => $this->category, |
251
|
|
|
self::PAYLOAD_THREAD_ID_KEY => $this->threadId, |
252
|
|
|
] |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
$payload = array_merge($payload, $this->customValues); |
256
|
|
|
|
257
|
|
|
return $payload; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..