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