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