1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the AppleApnPush package |
7
|
|
|
* |
8
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Apple\ApnPush\Encoder; |
15
|
|
|
|
16
|
|
|
use Apple\ApnPush\Model\Alert; |
17
|
|
|
use Apple\ApnPush\Model\Aps; |
18
|
|
|
use Apple\ApnPush\Model\Payload; |
19
|
|
|
use Apple\ApnPush\Model\Sound; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The encoder for encode notification payload to string for next send to Apple Push Notification Service |
23
|
|
|
*/ |
24
|
|
|
class PayloadEncoder implements PayloadEncoderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function encode(Payload $payload): string |
30
|
|
|
{ |
31
|
|
|
$data = [ |
32
|
|
|
'aps' => $this->convertApsToArray($payload->getAps()), |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$data = \array_merge($payload->getCustomData(), $data); |
36
|
|
|
|
37
|
|
|
return \json_encode($data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Convert APS data to array |
42
|
|
|
* |
43
|
|
|
* @param Aps $aps |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
private function convertApsToArray(Aps $aps): array |
48
|
|
|
{ |
49
|
|
|
$data = []; |
50
|
|
|
|
51
|
|
|
if ($aps->getAlert()) { |
52
|
|
|
$data['alert'] = $this->convertAlertToArray($aps->getAlert()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($aps->getSound()) { |
56
|
|
|
$sound = $aps->getSound(); |
57
|
|
|
|
58
|
|
|
if ($sound instanceof Sound) { |
59
|
|
|
$data['sound'] = [ |
60
|
|
|
'critical' => $sound->isCritical() ? 1 : 0, |
61
|
|
|
'name' => $sound->getName(), |
62
|
|
|
'volume' => $sound->getVolume(), |
63
|
|
|
]; |
64
|
|
|
} else { |
65
|
|
|
// Sound pass as string |
66
|
|
|
$data['sound'] = $sound; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($aps->getBadge() !== null) { |
71
|
|
|
$data['badge'] = $aps->getBadge(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($aps->getCategory()) { |
75
|
|
|
$data['category'] = $aps->getCategory(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($aps->isContentAvailable()) { |
79
|
|
|
$data['content-available'] = 1; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($aps->isMutableContent()) { |
83
|
|
|
$data['mutable-content'] = 1; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($aps->getThreadId()) { |
87
|
|
|
$data['thread-id'] = $aps->getThreadId(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Convert alert object to array |
95
|
|
|
* |
96
|
|
|
* @param Alert $alert |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
private function convertAlertToArray(Alert $alert): array |
101
|
|
|
{ |
102
|
|
|
$data = []; |
103
|
|
|
|
104
|
|
|
if ($alert->getBodyLocalized()->getKey()) { |
105
|
|
|
$data['loc-key'] = $alert->getBodyLocalized()->getKey(); |
106
|
|
|
$data['loc-args'] = $alert->getBodyLocalized()->getArgs(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($alert->getBody() || !$alert->getBodyLocalized()->getKey()) { |
110
|
|
|
$data['body'] = $alert->getBody(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($alert->getTitleLocalized()->getKey()) { |
114
|
|
|
$data['title-loc-key'] = $alert->getTitleLocalized()->getKey(); |
115
|
|
|
$data['title-loc-args'] = $alert->getTitleLocalized()->getArgs(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($alert->getTitle()) { |
119
|
|
|
$data['title'] = $alert->getTitle(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($alert->getActionLocalized()->getKey()) { |
123
|
|
|
$data['action-loc-key'] = $alert->getActionLocalized()->getKey(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($alert->getLaunchImage()) { |
127
|
|
|
$data['launch-image'] = $alert->getLaunchImage(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|