Skip to content

Commit b3ed289

Browse files
t1gorMinishlink
authored andcommitted
Add missing error documentation (#198)
1 parent eac6697 commit b3ed289

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,30 +192,40 @@ it as a parameter : `$webPush->flush($batchSize)`.
192192

193193
### Server errors
194194
You can see what the browser vendor's server sends back in case it encountered an error (push subscription expiration, wrong parameters...).
195-
`sendNotification()` (with flush as true) and `flush()` returns true if there were no errors. If there are errors it returns an array like the following.
196-
The `expired` key can be useful to clean your database of expired endpoints.
195+
`sendNotification()` (with `$flush` as `true`) and `flush()` **always** returns a [`\Generator`](http://php.net/manual/en/language.generators.php) with [`MessageSentReport`](https://github.com/web-push-libs/web-push-php/blob/master/src/MessageSentReport.php) objects, even if you just send one notification.
196+
To loop through the results, just pass it into `foreach`. You can also use [`iterator_to_array`](http://php.net/manual/en/function.iterator-to-array.php) to check the contents while debugging.
197197

198198
```php
199199
<?php
200-
$res = [
201-
[ // first notification (failed)
202-
'success' => false,
203-
'endpoint' => $theEndpointToDeleteInYourDatabaseIfExpired,
204-
'message' => $responseMessage,
205-
'statusCode' => $responseStatusCode,
206-
'headers' => $responseHeaders,
207-
'content' => $responseContent, // you may have more infos here
208-
'expired' => $isTheEndpointWrongOrExpired,
209-
],
210-
[ // second notification (succeeded)
211-
'success' => true,
212-
],
213-
[ // third notification
214-
...
215-
], ...
216-
];
200+
201+
/** @var \Minishlink\WebPush\MessageSentReport $report */
202+
foreach ($webPush->flush() as $report) {
203+
$endpoint = $report->getEndpoint();
204+
205+
if ($report->isSuccess()) {
206+
echo "[v] Message sent successfully for subscription {$endpoint}.";
207+
} else {
208+
echo "[x] Message failed to sent for subscription {$endpoint}: {$report->getReason()}";
209+
210+
// also available (to get more info)
211+
212+
/** @var \Psr\Http\Message\RequestInterface $requestToPushService */
213+
$requestToPushService = $report->getRequest();
214+
215+
/** @var \Psr\Http\Message\ResponseInterface $responseOfPushService */
216+
$responseOfPushService = $report->getResponse();
217+
218+
/** @var string $failReason */
219+
$failReason = $report->getReason();
220+
221+
/** @var bool $isTheEndpointWrongOrExpired */
222+
$isTheEndpointWrongOrExpired = $report->isSubscriptionExpired();
223+
}
224+
}
217225
```
218226

227+
**PLEASE NOTE:** You can only iterate **once** over the `\Generator` object.
228+
219229
Firefox errors are listed in the [autopush documentation](https://autopush.readthedocs.io/en/latest/http.html#errors).
220230

221231
### Payload length, security, and performance

0 commit comments

Comments
 (0)