diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d63aeaf..b1c8ebb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -76,4 +76,4 @@ jobs: composer-options: "--prefer-dist" - name: Run tests - run: vendor/bin/phpunit tests -v + run: vendor/bin/phpunit tests diff --git a/CHANGELOG.md b/CHANGELOG.md index d28ed54..a655b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Example/CompleteNotificationExample.php b/Example/CompleteNotificationExample.php index 0334460..8eb22db 100644 --- a/Example/CompleteNotificationExample.php +++ b/Example/CompleteNotificationExample.php @@ -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)); diff --git a/README.md b/README.md index c82d74f..c2e1286 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Api/Message/Message.php b/src/Api/Message/Message.php index 0761fab..5533ccb 100644 --- a/src/Api/Message/Message.php +++ b/src/Api/Message/Message.php @@ -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) { @@ -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; + } } diff --git a/src/Client/MessageClient.php b/src/Client/MessageClient.php index 13a9d26..6e90e09 100644 --- a/src/Client/MessageClient.php +++ b/src/Client/MessageClient.php @@ -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(); } diff --git a/tests/Api/Message/MessageTest.php b/tests/Api/Message/MessageTest.php index 9a19e34..540e1af 100644 --- a/tests/Api/Message/MessageTest.php +++ b/tests/Api/Message/MessageTest.php @@ -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); + } } diff --git a/tests/Client/MessageClientTest.php b/tests/Client/MessageClientTest.php index a76fb57..353f67f 100644 --- a/tests/Client/MessageClientTest.php +++ b/tests/Client/MessageClientTest.php @@ -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"); @@ -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']); } }