|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\Message\Type; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageBase; |
|
13
|
|
|
use Zbox\UnifiedPush\NotificationService\NotificationServices; |
|
14
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
|
15
|
|
|
use Zbox\UnifiedPush\Utils\JsonEncoder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class GCM |
|
19
|
|
|
* @package Zbox\UnifiedPush\Message\Type |
|
20
|
|
|
*/ |
|
21
|
|
|
class GCM extends MessageBase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The maximum size allowed for an Android notification payload is 4096 bytes |
|
25
|
|
|
*/ |
|
26
|
|
|
const PAYLOAD_MAX_LENGTH = 4096; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* It`s possible to send the same message to up to 1000 registration IDs in one request |
|
30
|
|
|
*/ |
|
31
|
|
|
const MAX_RECIPIENTS_PER_MESSAGE_COUNT = 1000; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Allow test the request without actually sending the message |
|
35
|
|
|
* |
|
36
|
|
|
* @var boolean |
|
37
|
|
|
*/ |
|
38
|
|
|
private $dryRun = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* If there is already a message with the same collapse key (and registration ID) |
|
42
|
|
|
* stored and waiting for delivery, the old message will be discarded |
|
43
|
|
|
* and the new message will take its place |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $collapseKey; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Message payload data |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private $payloadData; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* If the device is connected but idle, the message will still be delivered right away |
|
58
|
|
|
* unless the delay_while_idle flag is set to true |
|
59
|
|
|
* |
|
60
|
|
|
* @var boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
private $delayWhileIdle; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Package name of you application |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
private $packageName; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getMessageType() |
|
75
|
|
|
{ |
|
76
|
|
|
return NotificationServices::GOOGLE_CLOUD_MESSAGING; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getCollapseKey() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->collapseKey; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $collapseKey |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setCollapseKey($collapseKey) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->collapseKey = $collapseKey; |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return boolean |
|
99
|
|
|
*/ |
|
100
|
|
|
public function isDelayWhileIdle() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->delayWhileIdle; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param boolean $delayWhileIdle |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setDelayWhileIdle($delayWhileIdle) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->delayWhileIdle = $delayWhileIdle; |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isDryRun() |
|
119
|
|
|
{ |
|
120
|
|
|
return (bool) $this->dryRun; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param boolean $dryRun |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setDryRun($dryRun) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->dryRun = $dryRun; |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getPackageName() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->packageName; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $packageName |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setPackageName($packageName) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->packageName = $packageName; |
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getPayloadData() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->payloadData; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param array $payloadData |
|
161
|
|
|
* @return $this |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setPayloadData($payloadData) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->payloadData = $payloadData; |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
|
|
public function createPayload() |
|
173
|
|
|
{ |
|
174
|
|
|
$registrationIds = array(); |
|
175
|
|
|
|
|
176
|
|
|
foreach ($this->getRecipientCollection() as $recipient) { |
|
177
|
|
|
$registrationIds[] = $recipient->getIdentifier(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$payload = |
|
181
|
|
|
array( |
|
182
|
|
|
'collapse_key' => $this->getCollapseKey(), |
|
183
|
|
|
'delay_while_idle' => $this->isDelayWhileIdle(), |
|
184
|
|
|
'registration_ids' => $registrationIds, |
|
185
|
|
|
'data' => $this->getPayloadData(), |
|
186
|
|
|
'time_to_live' => $this->getExpirationTime()->format('U') - time() |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
return $this->checkIfDryRun($payload); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Pack message body into a json representation |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $payload |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function packPayload($payload) |
|
199
|
|
|
{ |
|
200
|
|
|
return JsonEncoder::jsonEncode($payload); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function validateRecipient($token) |
|
207
|
|
|
{ |
|
208
|
|
|
if (!preg_match('#^[0-9a-z\-\_]+$#i', $token)) { |
|
209
|
|
|
throw new InvalidArgumentException(sprintf( |
|
210
|
|
|
'Device token must be mask "%s". Token given: "%s"', |
|
211
|
|
|
'^[0-9a-z\-\_]+$#i', |
|
212
|
|
|
$token |
|
213
|
|
|
)); |
|
214
|
|
|
} |
|
215
|
|
|
return true; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param array $payload |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function checkIfDryRun($payload) |
|
223
|
|
|
{ |
|
224
|
|
|
if ($this->isDryRun()) { |
|
225
|
|
|
$payload['dry_run'] = $this->isDryRun(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $payload; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|