Skip to content

Commit eb5902d

Browse files
committed
Merge pull request #4 from Minishlink/TTL
Always include TTL header, fix #3
2 parents 4609f8b + 9dc7736 commit eb5902d

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ $webPush = new WebPush($apiKeys);
6464
$webPush->sendNotification($endpoint, null, null, true);
6565
```
6666

67+
### Time To Live
68+
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
69+
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.
70+
However, if you send multiple nonessential notifications, set a TTL of 0: the push notification will be delivered only
71+
if the user is currently connected. For other cases, you should use a minimum of one day if your users have multiple time
72+
zones, and if you don't several hours will suffice.
73+
74+
```php
75+
<?php
76+
77+
use Minishlink\WebPush\WebPush;
78+
79+
$webPush = new WebPush(); // default TTL is 4 weeks
80+
// send some important notifications...
81+
82+
$webPush->setTTL(3600);
83+
// send some not so important notifications
84+
85+
$webPush->setTTL(0);
86+
// send some trivial notifications
87+
```
88+
6789
### Changing the browser client
6890
By default, WebPush will use `MultiCurl`, allowing to send multiple notifications in parallel.
6991
You can change the client to any client extending `\Buzz\Client\AbstractClient`.

src/WebPush.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ class WebPush
3333
'GCM' => 'https://android.googleapis.com/gcm/send',
3434
);
3535

36+
/** @var int Time To Live of notifications */
37+
private $TTL;
38+
3639
/**
3740
* WebPush constructor.
3841
*
3942
* @param array $apiKeys Some servers needs authentication. Provide your API keys here. (eg. array('GCM' => 'GCM_API_KEY'))
40-
* @param int|null $TTL Time to live of notifications
43+
* @param int|null $TTL Time To Live of notifications, default being 4 weeks.
4144
* @param int|null $timeout Timeout of POST request
4245
* @param AbstractClient|null $client
4346
*/
44-
public function __construct(array $apiKeys = array(), $TTL = null, $timeout = null, AbstractClient $client = null)
47+
public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout = 30, AbstractClient $client = null)
4548
{
4649
$this->apiKeys = $apiKeys;
4750
$this->TTL = $TTL;
4851

4952
$client = isset($client) ? $client : new MultiCurl();
50-
$timeout = isset($timeout) ? $timeout : 30;
5153
$client->setTimeout($timeout);
5254
$this->browser = new Browser($client);
5355
}
@@ -155,14 +157,11 @@ private function sendToStandardEndpoints(array $notifications)
155157
{
156158
$headers = array(
157159
'Content-Length' => 0,
160+
'TTL' => $this->TTL,
158161
);
159162

160163
$content = '';
161164

162-
if (isset($this->TTL)) {
163-
$headers['TTL'] = $this->TTL;
164-
}
165-
166165
$responses = array();
167166
/** @var Notification $notification */
168167
foreach ($notifications as $notification) {
@@ -177,8 +176,11 @@ private function sendToGCMEndpoints(array $notifications)
177176
$maxBatchSubscriptionIds = 1000;
178177
$url = $this->urlByServerType['GCM'];
179178

180-
$headers['Authorization'] = 'key='.$this->apiKeys['GCM'];
181-
$headers['Content-Type'] = 'application/json';
179+
$headers = array(
180+
'Authorization' => 'key='.$this->apiKeys['GCM'],
181+
'Content-Type' => 'application/json',
182+
'TTL' => $this->TTL,
183+
);
182184

183185
$subscriptionIds = array();
184186
/** @var Notification $notification */
@@ -254,4 +256,20 @@ public function setBrowser($browser)
254256
{
255257
$this->browser = $browser;
256258
}
259+
260+
/**
261+
* @return int
262+
*/
263+
public function getTTL()
264+
{
265+
return $this->TTL;
266+
}
267+
268+
/**
269+
* @param int $TTL
270+
*/
271+
public function setTTL($TTL)
272+
{
273+
$this->TTL = $TTL;
274+
}
257275
}

0 commit comments

Comments
 (0)