1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use Pushok\Client; |
7
|
|
|
use Pushok\Notification as PushNotification; |
8
|
|
|
|
9
|
|
|
class ApnChannel |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The sandbox environment identifier. |
13
|
|
|
* |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
const SANDBOX = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The production environment identifier. |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
const PRODUCTION = 1; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The APNS client. |
27
|
|
|
* |
28
|
|
|
* @var \Pushok\Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The responses from the each notification sent. |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $responses; |
37
|
1 |
|
|
38
|
|
|
/** |
39
|
1 |
|
* Create a new channel instance. |
40
|
1 |
|
* |
41
|
|
|
* @param \Pushok\Client $client |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Client $client) |
44
|
|
|
{ |
45
|
|
|
$this->client = $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
/** |
49
|
|
|
* Send the notification to Apple Push Notification Service. |
50
|
1 |
|
* |
51
|
|
|
* @param mixed $notifiable |
52
|
1 |
|
* @param \Illuminate\Notifications\Notification $notification |
53
|
|
|
*/ |
54
|
|
|
public function send($notifiable, Notification $notification) |
55
|
|
|
{ |
56
|
1 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn', $notification); |
57
|
|
|
|
58
|
1 |
|
if (empty($tokens)) { |
59
|
|
|
return; |
60
|
1 |
|
} |
61
|
1 |
|
|
62
|
|
|
$message = $notification->toApn($notifiable); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$payload = (new ApnAdapter)->adapt($message); |
65
|
|
|
|
66
|
|
|
$this->sendNotifications($tokens, $payload); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
1 |
|
* Returns an array of ApnsResponseInterfaces from the most recent sending of push notifications. |
71
|
|
|
* @return array |
72
|
1 |
|
*/ |
73
|
|
|
public function retrieveResponses(){ |
74
|
1 |
|
return $this->responses; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
1 |
|
* Send the notification to each of the provided tokens. |
79
|
|
|
* |
80
|
1 |
|
* @param array $tokens |
81
|
1 |
|
* @param \Pushok\Payload $payload |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function sendNotifications($tokens, $payload) |
85
|
|
|
{ |
86
|
|
|
$notifications = []; |
87
|
|
|
|
88
|
|
|
foreach ($tokens as $token) { |
89
|
|
|
$notifications[] = new PushNotification($payload, $token); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->client->addNotifications($notifications); |
93
|
|
|
|
94
|
|
|
$this->responses = $this->client->push(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.