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
|
|
|
* Create a new channel instance. |
34
|
|
|
* |
35
|
|
|
* @param \Pushok\Client $client |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct(Client $client) |
38
|
|
|
{ |
39
|
1 |
|
$this->client = $client; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Send the notification to Apple Push Notification Service. |
44
|
|
|
* |
45
|
|
|
* @param mixed $notifiable |
46
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
47
|
|
|
*/ |
48
|
1 |
|
public function send($notifiable, Notification $notification) |
49
|
|
|
{ |
50
|
1 |
|
$tokens = (array) $notifiable->routeNotificationFor('apn', $notification); |
51
|
|
|
|
52
|
1 |
|
if (empty($tokens)) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$message = $notification->toApn($notifiable); |
|
|
|
|
57
|
|
|
|
58
|
1 |
|
$payload = (new ApnAdapter)->adapt($message); |
59
|
|
|
|
60
|
1 |
|
$responses = $this->sendNotifications($tokens, $payload); |
61
|
1 |
|
$this->storeResponses($notifiable, $responses); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Send the notification to each of the provided tokens. |
66
|
|
|
* |
67
|
|
|
* @param array $tokens |
68
|
|
|
* @param \Pushok\Payload $payload |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
1 |
|
protected function sendNotifications($tokens, $payload) |
72
|
|
|
{ |
73
|
1 |
|
$notifications = []; |
74
|
|
|
|
75
|
1 |
|
foreach ($tokens as $token) { |
76
|
1 |
|
$notifications[] = new PushNotification($payload, $token); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$this->client->addNotifications($notifications); |
80
|
|
|
|
81
|
1 |
|
return $this->client->push(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Store the responses from sending the push notification into the notifiable. |
86
|
|
|
* |
87
|
|
|
* @param $notifiable |
88
|
|
|
* @param $responses |
89
|
|
|
*/ |
90
|
1 |
|
private function storeResponses($notifiable, $responses) |
91
|
|
|
{ |
92
|
1 |
|
if (method_exists($notifiable, 'storeResponses')) { |
93
|
|
|
$notifiable->storeResponses($responses); |
94
|
|
|
} |
95
|
1 |
|
} |
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.