Skip to content

Commit

Permalink
Merge pull request #4 from Minishlink/TTL
Browse files Browse the repository at this point in the history
Always include TTL header, fix #3
  • Loading branch information
Minishlink committed Feb 20, 2016
2 parents 4609f8b + 9dc7736 commit eb5902d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

use Minishlink\WebPush\WebPush;

$webPush = new WebPush(); // default TTL is 4 weeks
// send some important notifications...

$webPush->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`.
Expand Down
36 changes: 27 additions & 9 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}
}

0 comments on commit eb5902d

Please sign in to comment.