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\Notification\Type; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\Type\GCM as GCMMessage; |
13
|
|
|
use Zbox\UnifiedPush\Utils\JsonEncoder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class GCM |
17
|
|
|
* @package Zbox\UnifiedPush\Message\Type |
18
|
|
|
*/ |
19
|
|
|
class GCM |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The maximum size allowed for an Android notification payload is 4096 bytes |
23
|
|
|
*/ |
24
|
|
|
const PAYLOAD_MAX_LENGTH = 4096; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param GCMMessage $message |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function createPayload(GCMMessage $message) |
31
|
|
|
{ |
32
|
|
|
$registrationIds = array(); |
33
|
|
|
|
34
|
|
|
foreach ($message->getRecipientCollection() as $recipient) { |
35
|
|
|
$registrationIds[] = $recipient->getIdentifier(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$payload = |
39
|
|
|
array( |
40
|
|
|
'collapse_key' => $message->getCollapseKey(), |
41
|
|
|
'delay_while_idle' => $message->isDelayWhileIdle(), |
42
|
|
|
'registration_ids' => $registrationIds, |
43
|
|
|
'data' => $message->getPayloadData(), |
44
|
|
|
'time_to_live' => $message->getExpirationTime()->format('U') - time() |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
return $this->checkIfDryRun($message, $payload); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Pack message body into a json representation |
52
|
|
|
* |
53
|
|
|
* @param array $payload |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function packPayload($payload) |
57
|
|
|
{ |
58
|
|
|
return JsonEncoder::jsonEncode($payload); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param GCMMessage $message |
63
|
|
|
* @param array $payload |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function checkIfDryRun(GCMMessage $message, $payload) |
67
|
|
|
{ |
68
|
|
|
if ($message->isDryRun()) { |
69
|
|
|
$payload['dry_run'] = $message->isDryRun(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $payload; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|