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