|
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
|
|
|
/** |
|
20
|
|
|
* The sandbox environment identifier. |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
const SANDBOX = 0; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The production environment identifier. |
|
28
|
|
|
* |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
const PRODUCTION = 1; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \ZendService\Apple\Apns\Client\Message */ |
|
34
|
|
|
protected $client; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \Illuminate\Events\Dispatcher */ |
|
37
|
|
|
protected $events; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \NotificationChannels\Apn\ApnCredentials */ |
|
40
|
|
|
protected $credentials; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create a new instance of the APN channel. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \ZendService\Apple\Apns\Client\Message $client |
|
46
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
|
47
|
|
|
* @param \NotificationChannels\Apn\ApnCredentials $credentials |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function __construct(Client $client, Dispatcher $events, ApnCredentials $credentials) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->client = $client; |
|
52
|
2 |
|
$this->events = $events; |
|
53
|
2 |
|
$this->credentials = $credentials; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Send the notification to Apple Push Notification Service. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $notifiable |
|
60
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function send($notifiable, Notification $notification) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn'); |
|
65
|
2 |
|
if (empty($tokens)) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$message = $notification->toApn($notifiable); |
|
|
|
|
|
|
70
|
2 |
|
if (! $message) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
$this->openConnection(); |
|
75
|
|
|
|
|
76
|
2 |
|
foreach ($tokens as $token) { |
|
77
|
2 |
|
$packet = $this->getPacket($message, $token); |
|
78
|
2 |
|
$this->sendPacket($notifiable, $notification, $packet, $token); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$this->closeConnection(); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the packet for the given message and token. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \NotificationChannels\Apn\ApnMessage $message |
|
88
|
|
|
* @param string |
|
89
|
|
|
* @return \ZendService\Apple\Apns\Message |
|
90
|
|
|
*/ |
|
91
|
2 |
|
protected function getPacket($message, $token) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$alert = new Alert(); |
|
94
|
2 |
|
$alert->setTitle($message->title); |
|
95
|
2 |
|
$alert->setBody($message->body); |
|
96
|
|
|
|
|
97
|
2 |
|
$packet = new Packet(); |
|
98
|
2 |
|
$packet->setToken($token); |
|
99
|
2 |
|
$packet->setBadge($message->badge); |
|
100
|
2 |
|
$packet->setSound($message->sound); |
|
101
|
2 |
|
$packet->setCategory($message->category); |
|
102
|
2 |
|
$packet->setContentAvailable($message->contentAvailable); |
|
103
|
2 |
|
$packet->setAlert($alert); |
|
104
|
2 |
|
$packet->setCustom($message->custom); |
|
105
|
|
|
|
|
106
|
2 |
|
return $packet; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Sent the notification to the given token. |
|
111
|
|
|
* |
|
112
|
|
|
* @param mixed $notifiable |
|
113
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
114
|
|
|
* @param \ZendService\Apple\Apns\Message $packet |
|
115
|
|
|
* @param string $token |
|
116
|
|
|
* @return void |
|
117
|
|
|
* @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
|
118
|
|
|
*/ |
|
119
|
2 |
|
protected function sendPacket($notifiable, $notification, $packet, $token) |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
2 |
|
$response = $this->client->send($packet); |
|
123
|
|
|
|
|
124
|
2 |
|
if ($response->getCode() !== Response::RESULT_OK) { |
|
125
|
1 |
|
$this->events->fire( |
|
126
|
1 |
|
new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
|
|
127
|
1 |
|
'token' => $token, |
|
128
|
2 |
|
'error' => $response->getCode(), |
|
129
|
|
|
]) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} catch (Exception $e) { |
|
133
|
|
|
throw SendingFailed::create($e); |
|
134
|
|
|
} |
|
135
|
2 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.