<?php

require __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('UTC');
use LeonardoTeixeira\Pushover\Client;
use LeonardoTeixeira\Pushover\Message;
use LeonardoTeixeira\Pushover\Priority;
use LeonardoTeixeira\Pushover\Sound;
use LeonardoTeixeira\Pushover\Exceptions\PushoverException;
$client = new Client('YOUR_USER_CODE_HERE', 'YOUR_TOKEN_HERE');
$message = new Message();
$message->setMessage('Your messsage <b>here</b>.');
$message->setTitle('Title here');
$message->setUrl('http://www.example.com/');
$message->setUrlTitle('Click me!');
$message->setPriority(Priority::HIGH);
$message->setSound(Sound::SIREN);
$message->setHtml(true);
$message->setDate(new \DateTime());
try {
    $client->push($message);
    echo 'The message has been pushed!', PHP_EOL;
} catch (PushoverException $e) {
    echo 'ERROR: ', $e->getMessage(), PHP_EOL;
}
Exemplo n.º 2
0
 public function push(Message $message, $device = null)
 {
     if (!$message instanceof Message) {
         throw new PushoverException('The parameter \'$message\' must be a Message instance.');
     }
     if ($message->getMessage() == null) {
         throw new PushoverException('The message content was not set.');
     }
     $postData = ['user' => $this->user, 'token' => $this->token, 'message' => $message->getMessage(), 'priority' => $message->getPriority()];
     if ($device != null) {
         $postData['device'] = $device;
     }
     if ($message->hasTitle()) {
         $postData['title'] = $message->getTitle();
     }
     if ($message->hasUrl()) {
         $postData['url'] = $message->getUrl();
     }
     if ($message->hasUrlTitle()) {
         $postData['url_title'] = $message->getUrlTitle();
     }
     if ($message->hasSound()) {
         $postData['sound'] = $message->getSound();
     }
     if ($message->hasHtml()) {
         $postData['html'] = $message->getHtml();
     }
     if ($message->hasDate()) {
         $postData['timestamp'] = $message->getDate()->getTimestamp();
     }
     try {
         $request = Requests::post(self::API_MESSAGE_URL, [], $postData);
         $responseJson = json_decode($request->body);
         if (!isset($responseJson->status) || $responseJson->status != 1) {
             if (isset($responseJson->errors)) {
                 throw new PushoverException($responseJson->errors[0]);
             } else {
                 throw new PushoverException('Unable to access the Pushover API.');
             }
         }
     } catch (\Exception $e) {
         throw new PushoverException($e->getMessage());
     }
 }