|
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\Message\Type\APNS as APNSMessage; |
|
14
|
|
|
use Zbox\UnifiedPush\Notification\PayloadHandler; |
|
15
|
|
|
use Zbox\UnifiedPush\Utils\JsonEncoder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class APNS |
|
19
|
|
|
* @package Zbox\UnifiedPush\Notification\PayloadHandler |
|
20
|
|
|
*/ |
|
21
|
|
|
class APNS extends PayloadHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The maximum size allowed for an iOS notification payload is 2 kilobytes |
|
25
|
|
|
* Prior to iOS 8 and in OS X, the maximum payload size is 256 bytes |
|
26
|
|
|
*/ |
|
27
|
|
|
const PAYLOAD_MAX_LENGTH = 2048; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param MessageInterface $message |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function isSupported(MessageInterface $message) |
|
34
|
|
|
{ |
|
35
|
|
|
return $message instanceof APNSMessage; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function createPayload() |
|
42
|
|
|
{ |
|
43
|
|
|
/** @var APNSMessage $message */ |
|
44
|
|
|
$message = $this->message; |
|
45
|
|
|
|
|
46
|
|
|
$payload = array( |
|
47
|
|
|
'aps' => array( |
|
48
|
|
|
'alert' => $message->getAlert(), |
|
49
|
|
|
'badge' => $message->getBadge(), |
|
50
|
|
|
'sound' => $message->getSound(), |
|
51
|
|
|
'category' => $message->getCategory(), |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
if ($message->isContentAvailable() === true) { |
|
56
|
|
|
$payload['aps']['content-available'] = 1; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return array_merge($payload, $message->getCustomPayloadData()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Pack message body into binary string |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $payload |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function packPayload($payload) |
|
69
|
|
|
{ |
|
70
|
|
|
$payload = JsonEncoder::jsonEncode($payload); |
|
71
|
|
|
|
|
72
|
|
|
/** @var APNSMessage $message */ |
|
73
|
|
|
$message = $this->message; |
|
74
|
|
|
|
|
75
|
|
|
$recipientId = $message->getRecipientDeviceCollection()->current()->getIdentifier(); |
|
76
|
|
|
|
|
77
|
|
|
$messageRecipientId = $message->getMessageIdentifier() . '_' . $recipientId; |
|
78
|
|
|
|
|
79
|
|
|
$packedPayload = |
|
80
|
|
|
pack('C', 1). // Command push |
|
81
|
|
|
pack('N', $messageRecipientId). |
|
82
|
|
|
pack('N', $message->getExpirationTime()->format('U')). |
|
83
|
|
|
pack('n', 32). // Token binary length |
|
84
|
|
|
pack('H*', $recipientId); |
|
85
|
|
|
pack('n', strlen($payload)); |
|
86
|
|
|
|
|
87
|
|
|
$packedPayload .= $payload; |
|
88
|
|
|
|
|
89
|
|
|
return $packedPayload; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|