1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AppleApnPush package |
5
|
|
|
* |
6
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Apple\ApnPush\Encoder; |
13
|
|
|
|
14
|
|
|
use Apple\ApnPush\Model\ApsData; |
15
|
|
|
use Apple\ApnPush\Model\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The encoder for encode notification message to string for next send to Apple Push Notification Service |
19
|
|
|
*/ |
20
|
|
|
class MessageEncoder implements MessageEncoderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function encode(Message $message) : string |
26
|
|
|
{ |
27
|
|
|
$data = [ |
28
|
|
|
'aps' => $this->convertApsDataToArray($message->getApsData()), |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
$data = array_merge($message->getCustomData(), $data); |
32
|
|
|
|
33
|
|
|
return json_encode($data); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Convert APS data to array |
38
|
|
|
* |
39
|
|
|
* @param ApsData $apsData |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
private function convertApsDataToArray(ApsData $apsData) : array |
44
|
|
|
{ |
45
|
|
|
$data = []; |
46
|
|
|
|
47
|
|
|
if ($apsData->getBodyCustom()) { |
48
|
|
|
$data['alert'] = $apsData->getBodyCustom(); |
49
|
|
|
} else { |
50
|
|
|
$data['alert'] = $apsData->getBody(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($apsData->getSound()) { |
54
|
|
|
$data['sound'] = $apsData->getSound(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($apsData->getBadge()) { |
58
|
|
|
$data['badge'] = $apsData->getBadge(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($apsData->getCategory()) { |
62
|
|
|
$data['category'] = $apsData->getCategory(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($apsData->isContentAvailable()) { |
66
|
|
|
$data['content-available'] = 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $data; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|