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
|
|
|
|
15
|
|
|
class ApnChannel |
16
|
|
|
{ |
17
|
|
|
use InteractsWithConnection; |
18
|
|
|
|
19
|
|
|
const SANDBOX = 0; |
20
|
|
|
const PRODUCTION = 1; |
21
|
|
|
|
22
|
|
|
/** @var \ZendService\Apple\Apns\Client\Message */ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** @var \Illuminate\Events\Dispatcher */ |
26
|
|
|
protected $events; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \ZendService\Apple\Apns\Client\Message $client |
30
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
31
|
|
|
* @param string $environment |
32
|
|
|
* @param string $certificate |
33
|
|
|
* @param string|null $passPhrase |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct( |
36
|
|
|
Client $client, |
37
|
|
|
Dispatcher $events, |
38
|
|
|
$environment, |
39
|
|
|
$certificate, |
40
|
|
|
$passPhrase = null |
41
|
|
|
) { |
42
|
2 |
|
$this->client = $client; |
43
|
2 |
|
$this->events = $events; |
44
|
2 |
|
$this->environment = $environment; |
45
|
2 |
|
$this->certificate = $certificate; |
46
|
2 |
|
$this->passPhrase = $passPhrase; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Send the notification to Apple Push Notification Service. |
51
|
|
|
* |
52
|
|
|
* @param mixed $notifiable |
53
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
54
|
|
|
* |
55
|
|
|
* @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
56
|
|
|
*/ |
57
|
2 |
|
public function send($notifiable, Notification $notification) |
58
|
|
|
{ |
59
|
2 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn'); |
60
|
2 |
|
if (! $tokens) { |
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$message = $notification->toApn($notifiable); |
|
|
|
|
65
|
2 |
|
if (! $message) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->openConnection(); |
70
|
|
|
|
71
|
2 |
|
foreach ($tokens as $token) { |
72
|
|
|
try { |
73
|
2 |
|
$alert = new Alert(); |
74
|
2 |
|
$alert->setTitle($message->title); |
75
|
2 |
|
$alert->setBody($message->body); |
76
|
|
|
|
77
|
2 |
|
$packet = new Packet(); |
78
|
2 |
|
$packet->setToken($token); |
79
|
2 |
|
$packet->setBadge($message->badge); |
80
|
2 |
|
$packet->setSound($message->sound); |
81
|
2 |
|
$packet->setCategory($message->category); |
82
|
2 |
|
$packet->setContentAvailable($message->contentAvailable); |
83
|
2 |
|
$packet->setAlert($alert); |
84
|
2 |
|
$packet->setCustom($message->custom); |
85
|
|
|
|
86
|
2 |
|
$response = $this->client->send($packet); |
87
|
|
|
|
88
|
2 |
|
if ($response->getCode() !== Response::RESULT_OK) { |
89
|
1 |
|
$this->events->fire( |
90
|
1 |
|
new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
91
|
1 |
|
'token' => $token, |
92
|
2 |
|
'error' => $response->getCode(), |
93
|
|
|
]) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} catch (Exception $e) { |
97
|
2 |
|
throw SendingFailed::create($e); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->closeConnection(); |
102
|
2 |
|
} |
103
|
|
|
} |
104
|
|
|
|
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.