Skip to content

Commit 5fc6e04

Browse files
committed
Merge branch 'master' into payload
2 parents 761a9d2 + c25ea27 commit 5fc6e04

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/WebPush.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function __construct(array $apiKeys = array(), $TTL = null, $timeout = nu
6363
* @param string|null $userPublicKey
6464
* @param bool $flush If you want to flush directly (usually when you send only one notification)
6565
*
66-
* @return bool|array Return an array of information if $flush is set to true and the request has failed. Else return true.
66+
* @return bool|array Return an array of information if $flush is set to true and the request has failed.
67+
* Else return true.
6768
* @throws \ErrorException
6869
*/
6970
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $flush = false)
@@ -84,12 +85,17 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
8485
* Flush notifications. Triggers the requests.
8586
*
8687
* @return array|bool If there are no errors, return true.
87-
* Else return an array of information for each notification sent (success, statusCode, headers).
88+
* If there were no notifications in the queue, return false.
89+
* Else return an array of information for each notification sent (success, statusCode, headers).
8890
*
8991
* @throws \ErrorException
9092
*/
9193
public function flush()
9294
{
95+
if (empty($this->notificationsByServerType)) {
96+
return false;
97+
}
98+
9399
// if GCM is present, we should check for the API key
94100
if (array_key_exists('GCM', $this->notificationsByServerType)) {
95101
if (empty($this->apiKeys['GCM'])) {
@@ -142,6 +148,9 @@ public function flush()
142148
}
143149
}
144150

151+
// reset queue
152+
$this->notificationsByServerType = null;
153+
145154
return $completeSuccess ? true : $return;
146155
}
147156

tests/WebPushTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testSendNotification()
3838
{
3939
$res = $this->webPush->sendNotification($this->endpoints['standard'], null, null, true);
4040

41-
$this->assertTrue($res);
41+
$this->assertEquals($res, true);
4242
}
4343

4444
public function testSendNotificationWithPayload()
@@ -61,7 +61,19 @@ public function testSendNotifications()
6161

6262
$res = $this->webPush->flush();
6363

64-
$this->assertTrue($res);
64+
$this->assertEquals(true, $res);
65+
}
66+
67+
public function testFlush()
68+
{
69+
$this->webPush->sendNotification($this->endpoints['standard']);
70+
$this->assertEquals(true, $this->webPush->flush());
71+
72+
// queue has been reset
73+
$this->assertEquals(false, $this->webPush->flush());
74+
75+
$this->webPush->sendNotification($this->endpoints['standard']);
76+
$this->assertEquals(true, $this->webPush->flush());
6577
}
6678

6779
public function testSendGCMNotificationWithoutGCMApiKey()

0 commit comments

Comments
 (0)