Skip to content

v5.0.0

Compare
Choose a tag to compare
@Minishlink Minishlink released this 27 Nov 18:58
· 91 commits to master since this release

Breaking changes

You now need to iterate over the results of flush in order to actually send the notifications.

The way you handle the results of the flush() method should be changed. In v5 the flush always returns a Generator object. This means that you can still iterate over it, but you wouldn't be able to store it, at least not as-is. Using the example from the docs, the responses would now look different:

PLEASE NOTE:
\Generator is returned even if you only send one message.

BEFORE:

$res = [
    [ // first notification (failed)
        'success' => false,
        'endpoint' => $theEndpointToDeleteInYourDatabaseIfExpired,
        'message' => $responseMessage,
        'statusCode' => $responseStatusCode,
        'headers' => $responseHeaders,
        'content' => $responseContent, // you may have more infos here
        'expired' => $isTheEndpointWrongOrExpired,
    ],
    [ // second notification (succeeded)
        'success' => true,
    ],
    [ // third notification
        ...
    ], ...
];

AFTER:

var_dump($res); // \Generator

/** \Minishlink\WebPush\MessageSentReport */
foreach ($res as $result) {
    // you now have access to request & response objects

    /** @var \Psr\Http\Message\RequestInterface $request */
    $request = $result-> getRequest();
    /** @var \Psr\Http\Message\ResponseInterface $response */
    $response = $result->getResponse();

    if ($result->isSuccess()) {
        // process successful message sent
        $logger->log('Notification with payload %s successfully sent for endpoint %s.' [
            json_decode((string) $response->getBody()),
            $result->getEndpoint()
        ]);
    } else {
        // or a failed one - check expiration first
        if ($result->isSubscriptionExpired()) {
            // this is just an example code, not included in library!
            $db->markExpired($result->getEndpoint());
        } else {
            // process faulty message
            $logger->log('Notification failed: %s. Payload: %s, endpoint: %s' [
                $result->getReason(),
                json_decode((string) $response->getBody()),
                $result->getEndpoint()
            ]);
        }
    }
}