Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
use GuzzleHttp\Client;
use Illuminate\Config\Repository;
use Illuminate\Events\Dispatcher;
class ParseNotificationHandler
{
/**
* Config repository.
*
* @var \Illuminate\Config\Repository
*/
protected $config;
/**
* Guzzle client instance.
*
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* Construct a new event subscription.
*
* @param \GuzzleHttp\Client $client
*/
public function __construct(Repository $config, Client $client)
{
$this->config = $config;
$this->client = $client;
}
/**
* Send push notification.
*
* @param string $channel
* @param string $message
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function send($channel, $message)
{
$payload = json_encode([
'where' => ['channels' => $channel],
'data' => ['alert' => $message],
]);
$request = $this->client->createRequest('POST', 'https://api.parse.com/1/push', [
'body' => $payload,
]);
$request->setHeaders([
'X-Parse-Application-Id' => $this->config->get('services.parse.app'),
'X-Parse-REST-API-Key' => $this->config->get('services.parse.key'),
'Content-Type' => 'application/json',
]);
return $this->client->send($request);
}
/**
* Subscribe to event listener.
*
* @param \Illuminate\Events\Dispatcher $events
* @return void
*/
public function subscribe(Dispatcher $events)
{
$events->listen('push.notifier: send', 'ParseNotificationHandler@send');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment