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\PayloadHandler; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageInterface; |
13
|
|
|
use Zbox\UnifiedPush\Notification\PayloadHandler; |
14
|
|
|
use Zbox\UnifiedPush\Message\Type\GCM as GCMMessage; |
15
|
|
|
use Zbox\UnifiedPush\Utils\JsonEncoder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class GCM |
19
|
|
|
* @package Zbox\UnifiedPush\Notification\PayloadHandler |
20
|
|
|
*/ |
21
|
|
|
class GCM extends PayloadHandler |
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
|
|
|
* @param MessageInterface $message |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function isSupported(MessageInterface $message) |
33
|
|
|
{ |
34
|
|
|
return $message instanceof GCMMessage; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function createPayload() |
41
|
|
|
{ |
42
|
|
|
$registrationIds = array(); |
43
|
|
|
|
44
|
|
|
/** @var GCMMessage $message */ |
45
|
|
|
$message = $this->message; |
46
|
|
|
|
47
|
|
|
foreach ($message->getRecipientDeviceCollection() as $recipient) { |
48
|
|
|
$registrationIds[] = $recipient->getIdentifier(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$payload = |
52
|
|
|
array( |
53
|
|
|
'collapse_key' => $message->getCollapseKey(), |
54
|
|
|
'delay_while_idle' => $message->isDelayWhileIdle(), |
55
|
|
|
'registration_ids' => $registrationIds, |
56
|
|
|
'data' => $message->getPayloadData(), |
57
|
|
|
'time_to_live' => $message->getExpirationTime()->format('U') - time() |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $this->checkIfDryRun($message, $payload); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Pack message body into a json representation |
65
|
|
|
* |
66
|
|
|
* @param array $payload |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function packPayload($payload) |
70
|
|
|
{ |
71
|
|
|
return JsonEncoder::jsonEncode($payload); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param GCMMessage $message |
76
|
|
|
* @param array $payload |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function checkIfDryRun(GCMMessage $message, $payload) |
80
|
|
|
{ |
81
|
|
|
if ($message->isDryRun()) { |
82
|
|
|
$payload['dry_run'] = $message->isDryRun(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $payload; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|