1 | <?php |
||
15 | class ApnChannel |
||
16 | { |
||
17 | const SANDBOX = 0; |
||
18 | const PRODUCTION = 1; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $environment; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $certificate; |
||
25 | |||
26 | /** @var string|null */ |
||
27 | protected $passPhrase; |
||
28 | |||
29 | /** @var \ZendService\Apple\Apns\Client\Message */ |
||
30 | protected $client; |
||
31 | |||
32 | /** @var \Illuminate\Events\Dispatcher */ |
||
33 | protected $events; |
||
34 | |||
35 | /** |
||
36 | * @param \ZendService\Apple\Apns\Client\Message $client |
||
37 | * @param \Illuminate\Events\Dispatcher $events |
||
38 | * @param string $environment |
||
39 | * @param string $certificate |
||
40 | * @param string|null $passPhrase |
||
41 | */ |
||
42 | 1 | public function __construct(Client $client, Dispatcher $events, $environment, $certificate, $passPhrase = null) |
|
50 | |||
51 | /** |
||
52 | * Send the notification to Apple Push Notification Service. |
||
53 | * |
||
54 | * @param mixed $notifiable |
||
55 | * @param \Illuminate\Notifications\Notification $notification |
||
56 | * |
||
57 | * @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
||
58 | */ |
||
59 | 1 | public function send($notifiable, Notification $notification) |
|
60 | { |
||
61 | 1 | $tokens = (array) $notifiable->routeNotificationFor('apn'); |
|
62 | 1 | if (! $tokens) { |
|
63 | return; |
||
64 | } |
||
65 | |||
66 | 1 | $message = $notification->toApn($notifiable); |
|
67 | 1 | if (! $message) { |
|
68 | return; |
||
69 | } |
||
70 | |||
71 | 1 | if (! $this->openConnection()) { |
|
72 | return; |
||
73 | } |
||
74 | |||
75 | 1 | foreach ($tokens as $token) { |
|
76 | try { |
||
77 | 1 | $alert = new Alert(); |
|
78 | 1 | $alert->setTitle($message->title); |
|
79 | 1 | $alert->setBody($message->body); |
|
80 | |||
81 | 1 | $packet = new Packet(); |
|
82 | 1 | $packet->setToken($token); |
|
83 | 1 | $packet->setBadge($message->badge); |
|
84 | 1 | $packet->setSound($message->sound); |
|
85 | 1 | $packet->setAlert($alert); |
|
86 | 1 | $packet->setCustom($message->custom); |
|
87 | |||
88 | 1 | $response = $this->client->send($packet); |
|
89 | |||
90 | 1 | if ($response->getCode() !== Response::RESULT_OK) { |
|
91 | 1 | $this->events->fire( |
|
92 | 1 | new NotificationFailed($notifiable, $notification, $this, [ |
|
93 | 1 | 'token' => $token, |
|
94 | 1 | 'error' => $response->getCode(), |
|
95 | 1 | ]) |
|
96 | 1 | ); |
|
97 | 1 | } |
|
98 | 1 | } catch (Exception $e) { |
|
99 | throw SendingFailed::create($e); |
||
100 | } |
||
101 | 1 | } |
|
102 | |||
103 | 1 | $this->closeConnection(); |
|
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * Open the connection. |
||
108 | * |
||
109 | * @return bool |
||
110 | * |
||
111 | * @throws \NotificationChannels\Apn\Exceptions\ConnectionFailed |
||
112 | */ |
||
113 | 1 | private function openConnection() |
|
123 | |||
124 | /** |
||
125 | * Close the connection. |
||
126 | */ |
||
127 | 1 | private function closeConnection() |
|
131 | } |
||
132 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.