Skip to content

Commit 238159e

Browse files
committed
Merge branch 'master' into payload
Conflicts: README.md src/WebPush.php
2 parents 5fc6e04 + eb5902d commit 238159e

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
# WebPush [![Build Status](https://travis-ci.org/Minishlink/web-push.svg?branch=master)](https://travis-ci.org/Minishlink/web-push)
1+
# WebPush
22
> Web Push library for PHP
33
4+
[![Build Status](https://travis-ci.org/Minishlink/web-push.svg?branch=master)](https://travis-ci.org/Minishlink/web-push)
5+
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d60e8eea-aea1-4739-8ce0-a3c3c12c6ccf/mini.png)](https://insight.sensiolabs.com/projects/d60e8eea-aea1-4739-8ce0-a3c3c12c6ccf)
6+
47
## Installation
58
`composer require minishlink/web-push`
69

@@ -73,6 +76,28 @@ $webPush = new WebPush($apiKeys);
7376
$webPush->sendNotification($endpoint, null, null, true);
7477
```
7578

79+
### Time To Live
80+
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
81+
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.
82+
However, if you send multiple nonessential notifications, set a TTL of 0: the push notification will be delivered only
83+
if the user is currently connected. For other cases, you should use a minimum of one day if your users have multiple time
84+
zones, and if you don't several hours will suffice.
85+
86+
```php
87+
<?php
88+
89+
use Minishlink\WebPush\WebPush;
90+
91+
$webPush = new WebPush(); // default TTL is 4 weeks
92+
// send some important notifications...
93+
94+
$webPush->setTTL(3600);
95+
// send some not so important notifications
96+
97+
$webPush->setTTL(0);
98+
// send some trivial notifications
99+
```
100+
76101
### Changing the browser client
77102
By default, WebPush will use `MultiCurl`, allowing to send multiple notifications in parallel.
78103
You can change the client to any client extending `\Buzz\Client\AbstractClient`.

src/Notification.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ class Notification
1616
/** @var string */
1717
private $endpoint;
1818

19-
/** @var string|null */
20-
private $payload;
21-
22-
/** @var string|null Base64 encoded */
23-
private $userPublicKey;
24-
2519
public function __construct($endpoint, $payload, $userPublicKey)
2620
{
2721
$this->endpoint = $endpoint;

src/WebPush.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,23 @@ class WebPush
3636
'GCM' => 'https://android.googleapis.com/gcm/send',
3737
);
3838

39+
/** @var int Time To Live of notifications */
40+
private $TTL;
41+
3942
/**
4043
* WebPush constructor.
4144
*
4245
* @param array $apiKeys Some servers needs authentication. Provide your API keys here. (eg. array('GCM' => 'GCM_API_KEY'))
43-
* @param int|null $TTL Time to live of notifications
46+
* @param int|null $TTL Time To Live of notifications, default being 4 weeks.
4447
* @param int|null $timeout Timeout of POST request
4548
* @param AbstractClient|null $client
4649
*/
47-
public function __construct(array $apiKeys = array(), $TTL = null, $timeout = null, AbstractClient $client = null)
50+
public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout = 30, AbstractClient $client = null)
4851
{
4952
$this->apiKeys = $apiKeys;
5053
$this->TTL = $TTL;
5154

5255
$client = isset($client) ? $client : new MultiCurl();
53-
$timeout = isset($timeout) ? $timeout : 30;
5456
$client->setTimeout($timeout);
5557
$this->browser = new Browser($client);
5658
}
@@ -213,21 +215,19 @@ private function sendToStandardEndpoints(array $notifications)
213215
'Encryption-Key' => 'keyid=p256dh;dh='.$encrypted['localPublicKey'],
214216
'Encryption' => 'keyid=p256dh;salt='.$encrypted['salt'],
215217
'Content-Encoding' => 'aesgcm128',
218+
'TTL' => $this->TTL,
216219
);
217220

218221
$content = $encrypted['cipherText'];
219222
} else {
220223
$headers = array(
221224
'Content-Length' => 0,
225+
'TTL' => $this->TTL,
222226
);
223227

224228
$content = '';
225229
}
226230

227-
if (isset($this->TTL)) {
228-
$headers['TTL'] = $this->TTL;
229-
}
230-
231231
$responses[] = $this->sendRequest($notification->getEndpoint(), $headers, $content);
232232
}
233233

@@ -239,8 +239,11 @@ private function sendToGCMEndpoints(array $notifications)
239239
$maxBatchSubscriptionIds = 1000;
240240
$url = $this->urlByServerType['GCM'];
241241

242-
$headers['Authorization'] = 'key='.$this->apiKeys['GCM'];
243-
$headers['Content-Type'] = 'application/json';
242+
$headers = array(
243+
'Authorization' => 'key='.$this->apiKeys['GCM'],
244+
'Content-Type' => 'application/json',
245+
'TTL' => $this->TTL,
246+
);
244247

245248
$subscriptionIds = array();
246249
/** @var Notification $notification */
@@ -316,4 +319,20 @@ public function setBrowser($browser)
316319
{
317320
$this->browser = $browser;
318321
}
322+
323+
/**
324+
* @return int
325+
*/
326+
public function getTTL()
327+
{
328+
return $this->TTL;
329+
}
330+
331+
/**
332+
* @param int $TTL
333+
*/
334+
public function setTTL($TTL)
335+
{
336+
$this->TTL = $TTL;
337+
}
319338
}

0 commit comments

Comments
 (0)