|
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' => $this->populateAlert($message), |
|
49
|
|
|
'badge' => $message->getBadge(), |
|
50
|
|
|
'sound' => $message->getSound(), |
|
51
|
|
|
'category' => $message->getCategory(), |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$urlArgs = $message->getUrlArgs(); |
|
56
|
|
|
if (!empty($urlArgs)) { |
|
57
|
|
|
$payload['aps']['url-args'] = $urlArgs; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($message->isContentAvailable() === true) { |
|
61
|
|
|
$payload['aps']['content-available'] = 1; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($message->isMutableContent() === true) { |
|
65
|
|
|
$payload['aps']['mutable-content'] = 1; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return array_merge($payload, $message->getCustomPayloadData()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Pack message body into binary string |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $payload |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function packPayload($payload) |
|
78
|
|
|
{ |
|
79
|
|
|
$payload = JsonEncoder::jsonEncode($payload); |
|
80
|
|
|
|
|
81
|
|
|
/** @var APNSMessage $message */ |
|
82
|
|
|
$message = $this->message; |
|
83
|
|
|
|
|
84
|
|
|
$recipientId = $message->getRecipientDeviceCollection()->current()->getIdentifier(); |
|
85
|
|
|
|
|
86
|
|
|
$messageRecipientId = $this->notificationId . '_' . $recipientId; |
|
87
|
|
|
|
|
88
|
|
|
$packedPayload = |
|
89
|
|
|
pack('C', 1). // Command push |
|
90
|
|
|
pack('N', $messageRecipientId). |
|
91
|
|
|
pack('N', $message->getExpirationTime()->format('U')). |
|
92
|
|
|
pack('n', 32). // Token binary length |
|
93
|
|
|
pack('H*', $recipientId); |
|
94
|
|
|
pack('n', strlen($payload)); |
|
95
|
|
|
|
|
96
|
|
|
$packedPayload .= $payload; |
|
97
|
|
|
|
|
98
|
|
|
return $packedPayload; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param APNSMessage $message |
|
103
|
|
|
* @return array|string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function populateAlert(APNSMessage $message) |
|
106
|
|
|
{ |
|
107
|
|
|
$dict = $message->getAlertDictionary(); |
|
108
|
|
|
|
|
109
|
|
|
if (is_null($dict)) { |
|
110
|
|
|
return $message->getAlert(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$alert = array( |
|
114
|
|
|
'body' => $dict->getBody(), |
|
115
|
|
|
'title' => $dict->getTitle(), |
|
116
|
|
|
'title-loc-key' => $dict->getTitleLocKey(), |
|
117
|
|
|
'title-loc-args' => $dict->getTitleLocArgs(), |
|
118
|
|
|
'action-loc-key' => $dict->getActionLocKey(), |
|
119
|
|
|
'loc-key' => $dict->getLocKey(), |
|
120
|
|
|
'loc-args' => $dict->getLocArgs(), |
|
121
|
|
|
'launch-image' => $dict->getLaunchImage() |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
return array_filter($alert); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|