1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Events\Dispatcher; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use ZendService\Apple\Apns\Client\Message as Client; |
9
|
|
|
use NotificationChannels\Apn\Exceptions\SendingFailed; |
10
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
11
|
|
|
use ZendService\Apple\Apns\Response\Message as Response; |
12
|
|
|
|
13
|
|
|
class ApnChannel |
14
|
|
|
{ |
15
|
|
|
use InteractsWithConnection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The sandbox environment identifier. |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
const SANDBOX = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The production environment identifier. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
const PRODUCTION = 1; |
30
|
|
|
|
31
|
|
|
/** @var \ZendService\Apple\Apns\Client\Message */ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** @var \Illuminate\Events\Dispatcher */ |
35
|
|
|
protected $events; |
36
|
|
|
|
37
|
|
|
/** @var \NotificationChannels\Apn\ApnCredentials */ |
38
|
|
|
protected $credentials; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new instance of the APN channel. |
42
|
|
|
* |
43
|
|
|
* @param \ZendService\Apple\Apns\Client\Message $client |
44
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
45
|
|
|
* @param \NotificationChannels\Apn\ApnCredentials $credentials |
46
|
|
|
*/ |
47
|
2 |
|
public function __construct( |
48
|
|
|
Client $client, |
49
|
|
|
Dispatcher $events, |
50
|
|
|
ApnAdapter $adapter, |
51
|
|
|
ApnCredentials $credentials |
52
|
|
|
) { |
53
|
2 |
|
$this->client = $client; |
54
|
2 |
|
$this->events = $events; |
55
|
2 |
|
$this->adapter = $adapter; |
|
|
|
|
56
|
2 |
|
$this->credentials = $credentials; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Send the notification to Apple Push Notification Service. |
61
|
|
|
* |
62
|
|
|
* @param mixed $notifiable |
63
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
64
|
|
|
*/ |
65
|
2 |
|
public function send($notifiable, Notification $notification) |
66
|
|
|
{ |
67
|
2 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn'); |
68
|
2 |
|
if (empty($tokens)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$message = $notification->toApn($notifiable); |
|
|
|
|
73
|
2 |
|
if (! $message) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
$this->sendNotifications($notifiable, $notification, $tokens, $message); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Send the notification to each of the provided tokens. |
82
|
|
|
* |
83
|
|
|
* @param mixed $notifiable |
84
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
85
|
|
|
*/ |
86
|
2 |
|
protected function sendNotifications($notifiable, $notification, $tokens, $message) |
87
|
|
|
{ |
88
|
2 |
|
foreach ($tokens as $token) { |
89
|
2 |
|
$this->openConnection(); |
90
|
|
|
|
91
|
2 |
|
$this->sendNotification( |
92
|
2 |
|
$notifiable, |
93
|
2 |
|
$notification, |
94
|
2 |
|
$this->adapter->adapt($message, $token), |
95
|
2 |
|
$token |
96
|
|
|
); |
97
|
|
|
|
98
|
2 |
|
$this->closeConnection(); |
99
|
|
|
} |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sent the notification to the given token. |
104
|
|
|
* |
105
|
|
|
* @param mixed $notifiable |
106
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
107
|
|
|
* @param \ZendService\Apple\Apns\Message $message |
108
|
|
|
* @param string $token |
109
|
|
|
* @return void |
110
|
|
|
* @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
111
|
|
|
*/ |
112
|
2 |
|
protected function sendNotification($notifiable, $notification, $message, $token) |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
2 |
|
$response = $this->client->send($message); |
116
|
|
|
|
117
|
2 |
|
if ($response->getCode() !== Response::RESULT_OK) { |
118
|
1 |
|
$this->events->fire( |
119
|
1 |
|
new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
120
|
1 |
|
'token' => $token, |
121
|
2 |
|
'error' => $response->getCode(), |
122
|
|
|
]) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} catch (Exception $e) { |
126
|
|
|
throw SendingFailed::create($e); |
127
|
|
|
} |
128
|
2 |
|
} |
129
|
|
|
} |
130
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: