diff --git a/README.md b/README.md index fb568547..d22cce9f 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,28 @@ $webPush = new WebPush($apiKeys); $webPush->sendNotification($endpoint, null, null, true); ``` +### Time To Live +Time To Live (TTL, in seconds) is how long a push message is retained by the push service (eg. Mozilla) in case the user browser +is not yet accessible (eg. is not connected). You may want to use a very long time for important notifications. The default TTL is 4 weeks. +However, if you send multiple nonessential notifications, set a TTL of 0: the push notification will be delivered only +if the user is currently connected. For other cases, you should use a minimum of one day if your users have multiple time +zones, and if you don't several hours will suffice. + +```php +setTTL(3600); +// send some not so important notifications + +$webPush->setTTL(0); +// send some trivial notifications +``` + ### Changing the browser client By default, WebPush will use `MultiCurl`, allowing to send multiple notifications in parallel. You can change the client to any client extending `\Buzz\Client\AbstractClient`. diff --git a/src/WebPush.php b/src/WebPush.php index 69c38210..f7984794 100644 --- a/src/WebPush.php +++ b/src/WebPush.php @@ -33,21 +33,23 @@ class WebPush 'GCM' => 'https://android.googleapis.com/gcm/send', ); + /** @var int Time To Live of notifications */ + private $TTL; + /** * WebPush constructor. * * @param array $apiKeys Some servers needs authentication. Provide your API keys here. (eg. array('GCM' => 'GCM_API_KEY')) - * @param int|null $TTL Time to live of notifications + * @param int|null $TTL Time To Live of notifications, default being 4 weeks. * @param int|null $timeout Timeout of POST request * @param AbstractClient|null $client */ - public function __construct(array $apiKeys = array(), $TTL = null, $timeout = null, AbstractClient $client = null) + public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout = 30, AbstractClient $client = null) { $this->apiKeys = $apiKeys; $this->TTL = $TTL; $client = isset($client) ? $client : new MultiCurl(); - $timeout = isset($timeout) ? $timeout : 30; $client->setTimeout($timeout); $this->browser = new Browser($client); } @@ -155,14 +157,11 @@ private function sendToStandardEndpoints(array $notifications) { $headers = array( 'Content-Length' => 0, + 'TTL' => $this->TTL, ); $content = ''; - if (isset($this->TTL)) { - $headers['TTL'] = $this->TTL; - } - $responses = array(); /** @var Notification $notification */ foreach ($notifications as $notification) { @@ -177,8 +176,11 @@ private function sendToGCMEndpoints(array $notifications) $maxBatchSubscriptionIds = 1000; $url = $this->urlByServerType['GCM']; - $headers['Authorization'] = 'key='.$this->apiKeys['GCM']; - $headers['Content-Type'] = 'application/json'; + $headers = array( + 'Authorization' => 'key='.$this->apiKeys['GCM'], + 'Content-Type' => 'application/json', + 'TTL' => $this->TTL, + ); $subscriptionIds = array(); /** @var Notification $notification */ @@ -254,4 +256,20 @@ public function setBrowser($browser) { $this->browser = $browser; } + + /** + * @return int + */ + public function getTTL() + { + return $this->TTL; + } + + /** + * @param int $TTL + */ + public function setTTL($TTL) + { + $this->TTL = $TTL; + } }