1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
4
|
|
|
|
5
|
|
|
use Pushok\Notification; |
6
|
|
|
use Pushok\Payload; |
7
|
|
|
use Pushok\Payload\Alert; |
8
|
|
|
|
9
|
|
|
class ApnAdapter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Convert an ApnMessage instance into a Zend Apns Message. |
13
|
|
|
* |
14
|
|
|
* @param \NotificationChannels\Apn\ApnMessage $message |
15
|
|
|
* @param string $token |
16
|
|
|
* @return \Pushok\Notification |
17
|
|
|
*/ |
18
|
18 |
|
public function adapt(ApnMessage $message, string $token) |
19
|
|
|
{ |
20
|
18 |
|
$alert = Alert::create(); |
21
|
|
|
|
22
|
18 |
|
if ($title = $message->title) { |
23
|
5 |
|
$alert->setTitle($title); |
24
|
|
|
} |
25
|
|
|
|
26
|
18 |
|
if ($body = $message->body) { |
27
|
1 |
|
$alert->setBody($body); |
28
|
|
|
} |
29
|
|
|
|
30
|
18 |
|
if ($titleLocArgs = $message->titleLocArgs) { |
31
|
|
|
$alert->setTitleLocArgs($titleLocArgs); |
32
|
|
|
} |
33
|
|
|
|
34
|
18 |
|
if ($titleLocKey = $message->titleLocKey) { |
35
|
|
|
$alert->setTitleLocKey($titleLocKey); |
36
|
|
|
} |
37
|
|
|
|
38
|
18 |
|
if ($actionLocKey = $message->actionLocKey) { |
39
|
|
|
$alert->setActionLocKey($actionLocKey); |
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
if ($locArgs = $message->locArgs) { |
43
|
|
|
$alert->setLocArgs($locArgs); |
44
|
|
|
} |
45
|
|
|
|
46
|
18 |
|
if ($locKey = $message->locKey) { |
47
|
|
|
$alert->setLocKey($locKey); |
48
|
|
|
} |
49
|
|
|
|
50
|
18 |
|
$payload = Payload::create() |
51
|
18 |
|
->setAlert($alert) |
52
|
18 |
|
->setContentAvailability((bool) $message->contentAvailable) |
53
|
18 |
|
->setMutableContent((bool) $message->mutableContent); |
54
|
|
|
|
55
|
18 |
|
if (is_int($badge = $message->badge)) { |
56
|
2 |
|
$payload->setBadge($badge); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
if ($sound = $message->sound) { |
60
|
1 |
|
$payload->setSound($sound); |
61
|
|
|
} |
62
|
|
|
|
63
|
18 |
|
if ($category = $message->category) { |
64
|
1 |
|
$payload->setCategory($category); |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
foreach ($message->custom as $key => $value) { |
68
|
1 |
|
$payload->setCustomValue($key, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
18 |
|
if ($pushType = $message->pushType) { |
72
|
4 |
|
$payload->setPushType($pushType); |
73
|
|
|
} |
74
|
|
|
|
75
|
18 |
|
$notification = new Notification($payload, $token); |
76
|
|
|
|
77
|
18 |
|
if ($expiresAt = $message->expiresAt) { |
78
|
1 |
|
$notification->setExpirationAt($expiresAt); |
79
|
|
|
} |
80
|
|
|
|
81
|
18 |
|
return $notification; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|