Minishlink\WebPush\WebPush::flush PHP Method

flush() public method

Flush notifications. Triggers the requests.
public flush ( ) : array | boolean
return array | boolean If there are no errors, return true. If there were no notifications in the queue, return false. Else return an array of information for each notification sent (success, statusCode, headers, content)
    public function flush()
    {
        if (empty($this->notifications)) {
            return false;
        }
        // for each endpoint server type
        $responses = $this->prepareAndSend($this->notifications);
        // if multi curl, flush
        if ($this->browser->getClient() instanceof MultiCurl) {
            /** @var MultiCurl $multiCurl */
            $multiCurl = $this->browser->getClient();
            $multiCurl->flush();
        }
        /** @var Response|null $response */
        $return = array();
        $completeSuccess = true;
        foreach ($responses as $i => $response) {
            if (!isset($response)) {
                $return[] = array('success' => false, 'endpoint' => $this->notifications[$i]->getEndpoint());
                $completeSuccess = false;
            } elseif (!$response->isSuccessful()) {
                $return[] = array('success' => false, 'endpoint' => $this->notifications[$i]->getEndpoint(), 'statusCode' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'content' => $response->getContent(), 'expired' => in_array($response->getStatusCode(), array(400, 404, 410)));
                $completeSuccess = false;
            } else {
                $return[] = array('success' => true);
            }
        }
        // reset queue
        $this->notifications = null;
        return $completeSuccess ? true : $return;
    }

Usage Example

Beispiel #1
0
        $webPush = new WebPush(array('GCM' => $push_api));
        $endpoints = get_endpoints("Voting_End30");
        // send multiple notifications
        foreach ($endpoints as $endpoint) {
            $webPush->sendNotification($endpoint['Endpoint']);
        }
        $webPush->flush();
    }
    if (in_array("Voting_End", get_event(3600))) {
        $to = get_emails("Voting_End60");
        $message = '
    <html>
    <body>
    <p>Dear HiveMember,</p>
    <p>You are yet to vote for this weeks film night. Voting closes in an hour.
    <p>Please <a href="https://films.jakestockwin.co.uk/voting.php">click here</a> to vote</p>
    <br>
    <p>Best wishes,<br>The HiveBot&trade;</p>
    </body>
    </html>
    ';
        mail($to, "Film Night Voting", $message, "Content-type:text/html");
        $webPush = new WebPush(array('GCM' => $push_api));
        $endpoints = get_endpoints("Voting_End60");
        // send multiple notifications
        foreach ($endpoints as $endpoint) {
            $webPush->sendNotification($endpoint['Endpoint']);
        }
        $webPush->flush();
    }
}