Skip to content

Commit

Permalink
Merge pull request #4 from lkaybob/patch/datetime-microsecond
Browse files Browse the repository at this point in the history
[fix] Issue #3 resolved
  • Loading branch information
lkaybob authored Jul 17, 2019
2 parents a5f638b + 3fe45ed commit f397061
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Config/APNsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace phpFCMv1\Config;

use DateInterval;
use DateTime;
use Exception;

class APNsConfig implements CommonConfig {
const PRIORITY_HIGH = '10', PRIORITY_NORMAL = '5';
private $payload;
Expand Down Expand Up @@ -45,11 +49,11 @@ function setPriority($priority) {
/**
* @param $time : Time for notification to live in seconds
* @return mixed : Expiration option using UNIX epoch date
* @throws \Exception
* @throws Exception
*/
function setTimeToLive($time) {
$expiration = new \DateTime('now');
$expiration -> add(new \DateInterval('PT' . $time . 'S'));
$expiration = DateTime::createFromFormat('U', $this->roundUpMilliseconds());
$expiration -> add(new DateInterval('PT' . $time . 'S'));
$expValue = $expiration -> format('U');

$payload = array_merge($this -> payload, array('apns-expiration' => $expValue));
Expand All @@ -71,4 +75,19 @@ public function getPayload() {
return $payload;
}
}

/**
* Path for PHP@7.2. Refer to the issue.
* https://github.com/lkaybob/php-fcm-v1/issues/3
* @return string
*/
private function roundUpMilliseconds() {
$converted = new DateTime('now');

if ($converted->format('u') != 0 && strpos(PHP_VERSION,'7.1') !== 0) {
$converted = $converted->add(new DateInterval('PT1S'));
}

return $converted->format('U');
}
}

0 comments on commit f397061

Please sign in to comment.