Skip to content

Commit

Permalink
Merge pull request #22 from slunak/ttl-feature
Browse files Browse the repository at this point in the history
Add ttl parameter
  • Loading branch information
slunak authored Jul 15, 2023
2 parents a24446e + fcd8d0e commit 99c44d4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:
composer-options: "--prefer-dist"

- name: Run tests
run: vendor/bin/phpunit tests -v
run: vendor/bin/phpunit tests
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Added

- Added support for Time to Live (TTL) parameter

## [1.3.2]

### Added
Expand Down
1 change: 1 addition & 0 deletions Example/CompleteNotificationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function completeNotification()
$message->setUrlTitle("Example URL");
$message->setisHtml(false);
$message->setTimestamp(new \DateTime('now'));
$message->setTtl(60 * 60 * 24); // 1 day
// assign priority to the notification
$message->setPriority(new Priority(Priority::NORMAL));

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Light, simple and fast, yet comprehensive wrapper for the [Pushover](https://pus
- Notification priority
- Notification sound (including custom sound)
- Message time
- Time to live
- User/Group Validation API ([Example](Example/UserGroupValidationExample.php))
- Validation by user or group key
- Validation by user and device
Expand Down
34 changes: 34 additions & 0 deletions src/Api/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ class Message
*/
private $timestamp;

/**
* Normally a message delivered to a device is retained on the device until it is deleted by the user,
* or is automatically deleted when the number of messages on the device exceeds the user's configured message limit.
* The ttl parameter specifies a Time to Live in seconds, after which the message will be automatically deleted from the devices it was delivered to.
* This can be useful for unimportant messages that have a limited usefulness after a short amount of time.
*
* The ttl parameter is ignored for messages with a priority value of 2.
*
* The ttl value must be a positive number of seconds, and is counted from the time the message is received by our API.
*
* @var int|null
*/
private $ttl;


public function __construct(string $message, string $title = null)
{
Expand Down Expand Up @@ -223,4 +237,24 @@ public function setTimestamp(\DateTime $timestamp): void
{
$this->timestamp = $timestamp;
}

/**
* @return int|null
*/
public function getTtl(): ?int
{
return $this->ttl;
}

/**
* @param int|null $ttl
*/
public function setTtl(?int $ttl): void
{
if ($ttl <= 0) {
throw new InvalidArgumentException('The ttl value of ' . $ttl . ' is invalid. The ttl value must be a positive number of seconds.');
}

$this->ttl = $ttl;
}
}
4 changes: 4 additions & 0 deletions src/Client/MessageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function buildCurlPostFields(Notification $notification): array
$curlPostFields['html'] = 1;
}

if (null !== $notification->getMessage()->getTtl()) {
$curlPostFields['ttl'] = $notification->getMessage()->getTtl();
}

if (null !== $notification->getSound()) {
$curlPostFields['sound'] = $notification->getSound()->getSound();
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Api/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,40 @@ public function testGetTimestamp()

$this->assertEquals($datetime->getTimestamp(), $message->getTimestamp());
}

public function testGetTtl()
{
$message = new Message("This is a test message");

$message->setTtl(3600);

$this->assertEquals(3600, $message->getTtl());
}

public function testSetTtl()
{
$message = new Message("This is a test message");

$message->setTtl(3600);

$this->assertEquals(3600, $message->getTtl());
}

public function testSetNegativeTtl()
{
$message = new Message("This is a test message");

$this->expectException(InvalidArgumentException::class);

$message->setTtl(-1);
}

public function testSetZeroTtl()
{
$message = new Message("This is a test message");

$this->expectException(InvalidArgumentException::class);

$message->setTtl(0);
}
}
2 changes: 2 additions & 0 deletions tests/Client/MessageClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testBuildCurlPostFields()
$message->setPriority(new Priority(Priority::EMERGENCY, 30, 300));
$message->getPriority()->setCallback("https://callback.example.com");
$message->setIsHtml(true);
$message->setTtl(60 * 60 * 24);

$recipient->addDevice("ios");
$recipient->addDevice("android");
Expand All @@ -65,6 +66,7 @@ public function testBuildCurlPostFields()
$this->assertEquals("300", $curlPostFields['expire']);
$this->assertEquals("https://callback.example.com", $curlPostFields['callback']);
$this->assertEquals("1", $curlPostFields['html']);
$this->assertEquals("86400", $curlPostFields['ttl']);
$this->assertEquals("pushover", $curlPostFields['sound']);
}
}

0 comments on commit 99c44d4

Please sign in to comment.