diff --git a/src/Config/APNsConfig.php b/src/Config/APNsConfig.php index 87dfbed..109b019 100644 --- a/src/Config/APNsConfig.php +++ b/src/Config/APNsConfig.php @@ -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; @@ -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)); @@ -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'); + } } \ No newline at end of file