Skip to content

Commit

Permalink
Merge pull request #78 from laravel-notification-channels/expires
Browse files Browse the repository at this point in the history
Adjust adapter to support more options
  • Loading branch information
dwightwatson authored Feb 21, 2020
2 parents 4e1a263 + c93ab6a commit 3707bfe
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
14 changes: 11 additions & 3 deletions src/ApnAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NotificationChannels\Apn;

use Pushok\Notification;
use Pushok\Payload;
use Pushok\Payload\Alert;

Expand All @@ -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();

Expand Down Expand Up @@ -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;
}
}
21 changes: 1 addition & 20 deletions src/ApnChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Notifications\Notification;
use Pushok\Client;
use Pushok\Notification as PushNotification;

class ApnChannel
{
Expand Down Expand Up @@ -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();
}
}
22 changes: 22 additions & 0 deletions src/ApnMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NotificationChannels\Apn;

use DateTime;
use Pushok\Client;

class ApnMessage
Expand Down Expand Up @@ -62,6 +63,13 @@ class ApnMessage
*/
public $pushType = null;

/**
* The expiration time of the notification.
*
* @var \DateTime|null
*/
public $expiresAt = null;

/**
* Message specific client.
*
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/ApnChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/ApnMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NotificationChannels\Apn\Tests;

use DateTime;
use Mockery;
use NotificationChannels\Apn\ApnMessage;
use Pushok\Client;
Expand Down Expand Up @@ -109,6 +110,19 @@ public function it_can_set_push_type()
$this->assertEquals($message, $result);
}

/** @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()
{
Expand Down

0 comments on commit 3707bfe

Please sign in to comment.