|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
6
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
|
7
|
|
|
use Illuminate\Notifications\Notification; |
|
8
|
|
|
use Pushok\Client; |
|
9
|
|
|
use Pushok\Response; |
|
10
|
|
|
|
|
11
|
|
|
class ApnChannel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The sandbox environment identifier. |
|
15
|
|
|
* |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
const SANDBOX = 0; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The production environment identifier. |
|
22
|
|
|
* |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
const PRODUCTION = 1; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The APNS client. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Pushok\Client |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $client; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The event dispatcher. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $events; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a new channel instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Pushok\Client $client |
|
45
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function __construct(Client $client, Dispatcher $events) |
|
48
|
|
|
{ |
|
49
|
2 |
|
$this->client = $client; |
|
50
|
2 |
|
$this->events = $events; |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Send the notification to Apple Push Notification Service. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $notifiable |
|
57
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
58
|
|
|
* @return array|void |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function send($notifiable, Notification $notification) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn', $notification); |
|
63
|
|
|
|
|
64
|
2 |
|
if (empty($tokens)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
$responses = $this->sendNotifications( |
|
69
|
2 |
|
$message->client ?? $this->client, |
|
70
|
2 |
|
$notification->toApn($notifiable), |
|
|
|
|
|
|
71
|
|
|
$tokens |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
2 |
|
$this->dispatchEvents($notifiable, $notification, $responses); |
|
75
|
|
|
|
|
76
|
2 |
|
return $responses; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Send the message to the given tokens through the given client. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Pushok\Client $client |
|
83
|
|
|
* @param \NotificationChannels\Apn\ApnMessage $message |
|
84
|
|
|
* @param array $tokens |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
2 |
|
protected function sendNotifications(Client $client, ApnMessage $message, array $tokens) |
|
88
|
|
|
{ |
|
89
|
2 |
|
foreach ($tokens as $token) { |
|
90
|
2 |
|
$client->addNotification((new ApnAdapter)->adapt($message, $token)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return $client->push(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Dispatch failed events for notifications that weren't delivered. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $notifiable |
|
100
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
101
|
|
|
* @param array $responses |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
2 |
|
protected function dispatchEvents($notifiable, $notification, array $responses) |
|
105
|
|
|
{ |
|
106
|
2 |
|
foreach ($responses as $response) { |
|
107
|
1 |
|
if ($response->getStatusCode() === Response::APNS_SUCCESS) { |
|
108
|
1 |
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$event = new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
|
|
112
|
1 |
|
'id' => $response->getApnsId(), |
|
113
|
1 |
|
'token' => $response->getDeviceToken(), |
|
114
|
1 |
|
'error' => $response->getErrorReason(), |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
1 |
|
$this->events->dispatch($event); |
|
118
|
|
|
} |
|
119
|
2 |
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.