Skip to content

Commit c25ea27

Browse files
committed
fix flush reset queue
1 parent 11c70ef commit c25ea27

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/WebPush.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function __construct(array $apiKeys = array(), $TTL = null, $timeout = nu
6060
* @param string|null $userPublicKey
6161
* @param bool $flush If you want to flush directly (usually when you send only one notification)
6262
*
63-
* @return bool|array Return an array of information if $flush is set to true and the request has failed. Else return true.
63+
* @return bool|array Return an array of information if $flush is set to true and the request has failed.
64+
* Else return true.
6465
* @throws \ErrorException
6566
*/
6667
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $flush = false)
@@ -81,12 +82,17 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
8182
* Flush notifications. Triggers the requests.
8283
*
8384
* @return array|bool If there are no errors, return true.
84-
* Else return an array of information for each notification sent (success, statusCode, headers).
85+
* If there were no notifications in the queue, return false.
86+
* Else return an array of information for each notification sent (success, statusCode, headers).
8587
*
8688
* @throws \ErrorException
8789
*/
8890
public function flush()
8991
{
92+
if (empty($this->notificationsByServerType)) {
93+
return false;
94+
}
95+
9096
// if GCM is present, we should check for the API key
9197
if (array_key_exists('GCM', $this->notificationsByServerType)) {
9298
if (empty($this->apiKeys['GCM'])) {
@@ -139,6 +145,9 @@ public function flush()
139145
}
140146
}
141147

148+
// reset queue
149+
$this->notificationsByServerType = null;
150+
142151
return $completeSuccess ? true : $return;
143152
}
144153

tests/WebPushTest.php

+14-2
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 testSendNotifications()
@@ -49,7 +49,19 @@ public function testSendNotifications()
4949

5050
$res = $this->webPush->flush();
5151

52-
$this->assertTrue($res);
52+
$this->assertEquals(true, $res);
53+
}
54+
55+
public function testFlush()
56+
{
57+
$this->webPush->sendNotification($this->endpoints['standard']);
58+
$this->assertEquals(true, $this->webPush->flush());
59+
60+
// queue has been reset
61+
$this->assertEquals(false, $this->webPush->flush());
62+
63+
$this->webPush->sendNotification($this->endpoints['standard']);
64+
$this->assertEquals(true, $this->webPush->flush());
5365
}
5466

5567
public function testSendGCMNotificationWithoutGCMApiKey()

0 commit comments

Comments
 (0)