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