Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
applemessage.php
1<?php
2
4
6{
7 protected const DEFAULT_PAYLOAD_MAXIMUM_SIZE = 2048;
8 protected const APPLE_RESERVED_NAMESPACE = 'aps';
9 protected const JSON_OPTIONS = JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE;
10
11 protected $_bAutoAdjustLongPayload = true;
12 protected $payloadMaxSize;
13
14 public function __construct($sDeviceToken = null, $maxPayloadSize = 2048)
15 {
16 if (isset($sDeviceToken))
17 {
18 $this->addRecipient($sDeviceToken);
19 }
20
21 $this->payloadMaxSize = (int)$maxPayloadSize ?: self::DEFAULT_PAYLOAD_MAXIMUM_SIZE;
22 }
23
24 public function setAutoAdjustLongPayload($bAutoAdjust)
25 {
26 $this->_bAutoAdjustLongPayload = (boolean)$bAutoAdjust;
27 }
28
29 public function getAutoAdjustLongPayload()
30 {
32 }
33
34 protected function getAlertData()
35 {
36 $this->text = $this->customProperties["senderMessage"] ?? $this->text;
37 unset($this->customProperties["senderMessage"]);
38 $this->title = $this->customProperties["senderName"] ?? $this->title ?? "";
39 unset($this->customProperties["senderName"]);
40 if ($this->text != null && $this->text != "")
41 {
42 return [
43 'body' => $this->text,
44 'title'=> $this->title,
45 ];
46 }
47
48 return [];
49 }
50
51 protected function _getPayload()
52 {
53 $alertData = $this->getAlertData();
54 if(!empty($alertData))
55 {
57 'alert' => $alertData
58 ];
59
60 $aPayload[self::APPLE_RESERVED_NAMESPACE]['mutable-content'] = 1;
61 }
62 else
63 {
64 $aPayload[self::APPLE_RESERVED_NAMESPACE]['content-available'] = 1;
65 }
66
67 if (isset($this->category))
68 {
69 $aPayload[self::APPLE_RESERVED_NAMESPACE]['category'] = (string)$this->category;
70 }
71
72 if (isset($this->badge) && $this->badge >= 0)
73 {
74 $aPayload[self::APPLE_RESERVED_NAMESPACE]['badge'] = (int)$this->badge;
75 }
76 if (isset($this->sound) && $this->sound <> '')
77 {
78 $aPayload[self::APPLE_RESERVED_NAMESPACE]['sound'] = (string)$this->sound;
79 }
80
81 if (is_array($this->customProperties))
82 {
83 foreach ($this->customProperties as $sPropertyName => $mPropertyValue)
84 {
85 $aPayload[$sPropertyName] = $mPropertyValue;
86 }
87 }
88
89 return $aPayload;
90 }
91
92 public function getPayload()
93 {
94 $sJSONPayload = str_replace(
95 '"' . self::APPLE_RESERVED_NAMESPACE . '":[]',
96 '"' . self::APPLE_RESERVED_NAMESPACE . '":{}',
97 json_encode($this->_getPayload(), static::JSON_OPTIONS)
98 );
99 $nJSONPayloadLen = strlen($sJSONPayload);
100 if ($nJSONPayloadLen <= $this->payloadMaxSize)
101 {
102 return $sJSONPayload;
103 }
104 if (!$this->_bAutoAdjustLongPayload)
105 {
106 return false;
107 }
108
110 $useSenderText = false;
111 if(array_key_exists("senderMessage", $this->customProperties))
112 {
113 $useSenderText = true;
114 $text = $this->customProperties["senderMessage"];
115 }
116 $nMaxTextLen = $nTextLen = strlen($text) - ($nJSONPayloadLen - $this->payloadMaxSize);
117 if ($nMaxTextLen <= 0)
118 {
119 return false;
120 }
121
122 while (strlen($text) > $nMaxTextLen)
123 {
124 $text = substr($text, 0, --$nTextLen);
125 }
126 if($useSenderText)
127 {
128 $this->setCustomProperty("senderMessage", $text);
129 }
130 else
131 {
132 $this->setText($text);
133 }
134 return $this->getPayload();
135 }
136
137 public function getBatch()
138 {
139 $arTokens = $this->getRecipients();
140 $sPayload = $this->getPayload();
141
142 if (!$sPayload)
143 {
144 return false;
145 }
146
147 $nPayloadLength = strlen($sPayload);
148 $totalBatch = "";
149 foreach ($arTokens as $token)
150 {
151 $sDeviceToken = $token;
152
153 $sRet = pack('CNNnH*',
154 1,
155 $this->getCustomIdentifier(),
156 $this->getExpiry() > 0 ? time() + $this->getExpiry() : 0,
157 32,
158 $sDeviceToken)
159 ;
160 $sRet .= pack('n', $nPayloadLength);
161 $sRet .= $sPayload;
162 if ($totalBatch <> '')
163 {
164 $totalBatch .= ";";
165 }
166 $totalBatch .= base64_encode($sRet);
167 }
168
169 return $totalBatch;
170 }
171
172}
__construct($sDeviceToken=null, $maxPayloadSize=2048)