|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SemyonChetvertnyh\ApnNotificationChannel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use Illuminate\Notifications\Notification; |
|
7
|
|
|
use Pushok\Client; |
|
8
|
|
|
use Pushok\Notification as PushokNotification; |
|
9
|
|
|
use Pushok\Payload; |
|
10
|
|
|
use Pushok\Payload\Alert; |
|
11
|
|
|
use SemyonChetvertnyh\ApnNotificationChannel\Exceptions\CouldNotSendNotification; |
|
12
|
|
|
use SemyonChetvertnyh\ApnNotificationChannel\Exceptions\InvalidPayloadException; |
|
13
|
|
|
|
|
14
|
|
|
class ApnChannel |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The Pushok Client instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Pushok\Client |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create an instance of APN channel. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \Pushok\Client $client |
|
27
|
|
|
* @return void |
|
|
|
|
|
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Client $client) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->client = $client; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Send the given notification. |
|
36
|
|
|
* |
|
37
|
|
|
* @param mixed $notifiable |
|
38
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
39
|
|
|
* @return void |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \SemyonChetvertnyh\ApnNotificationChannel\Exceptions\InvalidPayloadException |
|
42
|
|
|
* @throws \SemyonChetvertnyh\ApnNotificationChannel\Exceptions\CouldNotSendNotification |
|
43
|
|
|
*/ |
|
44
|
|
|
public function send($notifiable, Notification $notification) |
|
45
|
|
|
{ |
|
46
|
|
|
if (! $deviceTokens = $notifiable->routeNotificationFor('apn', $notification)) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->client->addNotifications( |
|
51
|
|
|
$this->notifications($notification->toApn($notifiable), $deviceTokens) |
|
|
|
|
|
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$responses = $this->client->push(); |
|
55
|
|
|
|
|
56
|
|
|
ApnsResponseCollection::make($responses) |
|
57
|
|
|
->onlyUnsuccessful() |
|
58
|
|
|
->unlessEmpty(function (ApnsResponseCollection $responses) { |
|
59
|
|
|
throw CouldNotSendNotification::withUnsuccessful($responses); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Format an array with notifications. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \SemyonChetvertnyh\ApnNotificationChannel\ApnMessage $message |
|
67
|
|
|
* @param \Illuminate\Contracts\Support\Arrayable|array $deviceTokens |
|
68
|
|
|
* @return \Pushok\Notification[] |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function notifications(ApnMessage $message, $deviceTokens) |
|
71
|
|
|
{ |
|
72
|
|
|
$deviceTokens = $deviceTokens instanceof Arrayable ? $deviceTokens->toArray() : $deviceTokens; |
|
73
|
|
|
|
|
74
|
|
|
return collect($deviceTokens)->map(function ($deviceToken) use ($message) { |
|
75
|
|
|
return new PushokNotification($this->payload($message), $deviceToken); |
|
76
|
|
|
})->all(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Format a payload. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \SemyonChetvertnyh\ApnNotificationChannel\ApnMessage $message |
|
83
|
|
|
* @return \Pushok\Payload |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \SemyonChetvertnyh\ApnNotificationChannel\Exceptions\InvalidPayloadException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function payload(ApnMessage $message) |
|
88
|
|
|
{ |
|
89
|
|
|
$payload = Payload::create() |
|
90
|
|
|
->setAlert($this->alert($message)); |
|
91
|
|
|
|
|
92
|
|
|
if ($isContentAvailable = $message->isContentAvailable()) { |
|
93
|
|
|
$payload->setContentAvailability($isContentAvailable); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($hasMutableContent = $message->hasMutableContent()) { |
|
97
|
|
|
$payload->setMutableContent($hasMutableContent); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (! is_null($message->badge)) { |
|
101
|
|
|
$payload->setBadge($message->badge); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (! is_null($message->sound)) { |
|
105
|
|
|
$payload->setSound($message->sound); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (! is_null($message->category)) { |
|
109
|
|
|
$payload->setCategory($message->category); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (! is_null($message->threadId)) { |
|
113
|
|
|
$payload->setThreadId($message->threadId); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
try { |
|
117
|
|
|
foreach ($message->custom as $key => $value) { |
|
118
|
|
|
$payload->setCustomValue($key, $value); |
|
119
|
|
|
} |
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
throw new InvalidPayloadException($e->getMessage()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $payload; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Format an alert. |
|
129
|
|
|
* |
|
130
|
|
|
* @param \SemyonChetvertnyh\ApnNotificationChannel\ApnMessage $message |
|
131
|
|
|
* @return \Pushok\Payload\Alert |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function alert(ApnMessage $message) |
|
134
|
|
|
{ |
|
135
|
|
|
$alert = Alert::create(); |
|
136
|
|
|
|
|
137
|
|
|
if ($message->title) { |
|
138
|
|
|
$alert->setTitle($message->title); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($message->subtitle) { |
|
142
|
|
|
$alert->setSubtitle($message->subtitle); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ($message->body) { |
|
146
|
|
|
$alert->setBody($message->body); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if ($message->launchImage) { |
|
150
|
|
|
$alert->setLaunchImage($message->launchImage); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if ($message->titleLocArgs) { |
|
154
|
|
|
$alert->setTitleLocArgs($message->titleLocArgs); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (! is_null($message->titleLocKey)) { |
|
158
|
|
|
$alert->setTitleLocKey($message->titleLocKey); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (! is_null($message->actionLocKey)) { |
|
162
|
|
|
$alert->setActionLocKey($message->actionLocKey); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (! empty($message->locArgs)) { |
|
166
|
|
|
$alert->setLocArgs($message->locArgs); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if ($message->locKey) { |
|
170
|
|
|
$alert->setLocKey($message->locKey); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $alert; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.