|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Events\Dispatcher; |
|
7
|
|
|
use ZendService\Apple\Apns\Message\Alert; |
|
8
|
|
|
use Illuminate\Notifications\Notification; |
|
9
|
|
|
use ZendService\Apple\Apns\Message as Packet; |
|
10
|
|
|
use ZendService\Apple\Apns\Client\Message as Client; |
|
11
|
|
|
use NotificationChannels\Apn\Exceptions\SendingFailed; |
|
12
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
|
13
|
|
|
use ZendService\Apple\Apns\Response\Message as Response; |
|
14
|
|
|
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient; |
|
15
|
|
|
use ZendService\Apple\Apns\Response\Feedback as FeedbackResponse; |
|
16
|
|
|
|
|
17
|
|
|
class ApnChannel |
|
18
|
|
|
{ |
|
19
|
|
|
const SANDBOX = 0; |
|
20
|
|
|
const PRODUCTION = 1; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $environment; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $certificate; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string|null */ |
|
29
|
|
|
protected $passPhrase; |
|
30
|
|
|
|
|
31
|
|
|
/** @var \ZendService\Apple\Apns\Client\Message */ |
|
32
|
|
|
protected $client; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \ZendService\Apple\Apns\Client\Feedback */ |
|
35
|
|
|
protected $feedbackClient; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \Illuminate\Events\Dispatcher */ |
|
38
|
|
|
protected $events; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \ZendService\Apple\Apns\Client\Message $client |
|
42
|
|
|
* @param \ZendService\Apple\Apns\Client\Feedback $feedbackClient |
|
43
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
|
44
|
|
|
* @param string $environment |
|
45
|
|
|
* @param string $certificate |
|
46
|
|
|
* @param string|null $passPhrase |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function __construct( |
|
49
|
|
|
Client $client, |
|
50
|
|
|
FeedbackClient $feedbackClient, |
|
51
|
|
|
Dispatcher $events, |
|
52
|
|
|
$environment, |
|
53
|
|
|
$certificate, |
|
54
|
|
|
$passPhrase = null |
|
55
|
|
|
) { |
|
56
|
2 |
|
$this->client = $client; |
|
57
|
2 |
|
$this->feedbackClient = $feedbackClient; |
|
58
|
2 |
|
$this->events = $events; |
|
59
|
2 |
|
$this->environment = $environment; |
|
60
|
2 |
|
$this->certificate = $certificate; |
|
61
|
2 |
|
$this->passPhrase = $passPhrase; |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Send the notification to Apple Push Notification Service. |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $notifiable |
|
68
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function send($notifiable, Notification $notification) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn'); |
|
75
|
1 |
|
if (! $tokens) { |
|
|
|
|
|
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
$message = $notification->toApn($notifiable); |
|
|
|
|
|
|
80
|
1 |
|
if (! $message) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$this->openConnection(); |
|
85
|
|
|
|
|
86
|
1 |
|
foreach ($tokens as $token) { |
|
87
|
|
|
try { |
|
88
|
1 |
|
$alert = new Alert(); |
|
89
|
1 |
|
$alert->setTitle($message->title); |
|
90
|
1 |
|
$alert->setBody($message->body); |
|
91
|
|
|
|
|
92
|
1 |
|
$packet = new Packet(); |
|
93
|
1 |
|
$packet->setToken($token); |
|
94
|
1 |
|
$packet->setBadge($message->badge); |
|
95
|
1 |
|
$packet->setSound($message->sound); |
|
96
|
1 |
|
$packet->setCategory($message->category); |
|
97
|
1 |
|
$packet->setContentAvailable($message->contentAvailable); |
|
98
|
1 |
|
$packet->setAlert($alert); |
|
99
|
1 |
|
$packet->setCustom($message->custom); |
|
100
|
|
|
|
|
101
|
1 |
|
$response = $this->client->send($packet); |
|
102
|
|
|
|
|
103
|
1 |
|
if ($response->getCode() !== Response::RESULT_OK) { |
|
104
|
1 |
|
$this->events->fire( |
|
105
|
1 |
|
new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
|
|
106
|
1 |
|
'token' => $token, |
|
107
|
1 |
|
'error' => $response->getCode(), |
|
108
|
|
|
]) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} catch (Exception $e) { |
|
112
|
1 |
|
throw SendingFailed::create($e); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
$this->closeConnection(); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get feedback from the Apple Feedback Service about failed deliveries. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array|ApnFeedback[] |
|
123
|
|
|
* @throws Exceptions\ConnectionFailed |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function getFeedback() |
|
126
|
|
|
{ |
|
127
|
1 |
|
$client = $this->feedbackClient; |
|
128
|
|
|
|
|
129
|
|
|
try { |
|
130
|
1 |
|
$client->open($this->environment, $this->certificate, $this->passPhrase); |
|
131
|
|
|
} catch (Exception $exception) { |
|
132
|
|
|
throw Exceptions\ConnectionFailed::create($exception); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
$feedback = []; |
|
136
|
|
|
|
|
137
|
|
|
/** @var FeedbackResponse $response */ |
|
138
|
1 |
|
foreach ($client->feedback() as $response) { |
|
139
|
1 |
|
$feedback[] = new ApnFeedback($response->getToken(), $response->getTime()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$client->close(); |
|
143
|
|
|
|
|
144
|
1 |
|
return $feedback; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Open the connection. |
|
149
|
|
|
* |
|
150
|
|
|
* @throws \NotificationChannels\Apn\Exceptions\ConnectionFailed |
|
151
|
|
|
*/ |
|
152
|
1 |
|
private function openConnection() |
|
153
|
|
|
{ |
|
154
|
|
|
try { |
|
155
|
1 |
|
$this->client->open($this->environment, $this->certificate, $this->passPhrase); |
|
156
|
|
|
} catch (Exception $exception) { |
|
157
|
|
|
throw Exceptions\ConnectionFailed::create($exception); |
|
158
|
|
|
} |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Close the connection. |
|
163
|
|
|
*/ |
|
164
|
1 |
|
private function closeConnection() |
|
165
|
|
|
{ |
|
166
|
1 |
|
$this->client->close(); |
|
167
|
1 |
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.