diff --git a/src/ApnAdapter.php b/src/ApnAdapter.php index fbdd2a6..df52499 100644 --- a/src/ApnAdapter.php +++ b/src/ApnAdapter.php @@ -2,6 +2,7 @@ namespace NotificationChannels\Apn; +use Pushok\Notification; use Pushok\Payload; use Pushok\Payload\Alert; @@ -11,9 +12,10 @@ class ApnAdapter * Convert an ApnMessage instance into a Zend Apns Message. * * @param \NotificationChannels\Apn\ApnMessage $message - * @return \Pushok\Payload + * @param string $token + * @return \Pushok\Notification */ - public function adapt(ApnMessage $message) + public function adapt(ApnMessage $message, string $token) { $alert = Alert::create(); @@ -46,6 +48,12 @@ public function adapt(ApnMessage $message) $payload->setCustomValue($key, $value); } - return $payload; + $notification = new Notification($payload, $token); + + if ($expiresAt = $message->expiresAt) { + $notification->setExpirationAt($expiresAt); + } + + return $notification; } } diff --git a/src/ApnChannel.php b/src/ApnChannel.php index 51078df..9282758 100644 --- a/src/ApnChannel.php +++ b/src/ApnChannel.php @@ -4,7 +4,6 @@ use Illuminate\Notifications\Notification; use Pushok\Client; -use Pushok\Notification as PushNotification; class ApnChannel { @@ -58,28 +57,10 @@ public function send($notifiable, Notification $notification) $client = $message->client ?? $this->client; - $payload = (new ApnAdapter)->adapt($message); - - return $this->sendNotifications($client, $tokens, $payload); - } - - /** - * Send the notification to each of the provided tokens. - * - * @param array $tokens - * @param \Pushok\Payload $payload - * @return array - */ - protected function sendNotifications($client, $tokens, $payload) - { - $notifications = []; - foreach ($tokens as $token) { - $notifications[] = new PushNotification($payload, $token); + $client->addNotification((new ApnAdapter)->adapt($message, $token)); } - $client->addNotifications($notifications); - return $client->push(); } } diff --git a/src/ApnMessage.php b/src/ApnMessage.php index bbf4286..db03212 100644 --- a/src/ApnMessage.php +++ b/src/ApnMessage.php @@ -2,6 +2,7 @@ namespace NotificationChannels\Apn; +use DateTime; use Pushok\Client; class ApnMessage @@ -62,6 +63,13 @@ class ApnMessage */ public $pushType = null; + /** + * The expiration time of the notification. + * + * @var \DateTime|null + */ + public $expiresAt = null; + /** * Message specific client. * @@ -201,6 +209,20 @@ public function pushType(string $pushType) return $this; } + /** + * Set the expiration time for the message. + * + * @param \DateTime $expiresAt + * + * @return $this + */ + public function expiresAt(DateTime $expiresAt) + { + $this->expiresAt = $expiresAt; + + return $this; + } + /** * Add custom data to the notification. * diff --git a/tests/ApnChannelTest.php b/tests/ApnChannelTest.php index f408156..32a2fd6 100644 --- a/tests/ApnChannelTest.php +++ b/tests/ApnChannelTest.php @@ -28,7 +28,7 @@ public function it_can_send_a_notification() { $message = $this->notification->toApn($this->notifiable); - $this->client->shouldReceive('addNotifications'); + $this->client->shouldReceive('addNotification'); $this->client->shouldReceive('push')->once(); $this->channel->send($this->notifiable, $this->notification); diff --git a/tests/ApnMessageTest.php b/tests/ApnMessageTest.php index c202152..9430e29 100644 --- a/tests/ApnMessageTest.php +++ b/tests/ApnMessageTest.php @@ -2,6 +2,7 @@ namespace NotificationChannels\Apn\Tests; +use DateTime; use Mockery; use NotificationChannels\Apn\ApnMessage; use Pushok\Client; @@ -101,6 +102,19 @@ public function it_can_set_push_type() $this->assertEquals('type', $message->pushType); } + /** @test */ + public function it_can_set_expires_at() + { + $message = new ApnMessage; + + $now = new DateTime; + + $result = $message->expiresAt($now); + + $this->assertEquals($now, $message->expiresAt); + $this->assertEquals($message, $result); + } + /** @test */ public function it_can_set_custom_value() {