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