Skip to content

Instantly share code, notes, and snippets.

@crynobone
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crynobone/56a6385863be221bfd88 to your computer and use it in GitHub Desktop.
Save crynobone/56a6385863be221bfd88 to your computer and use it in GitHub Desktop.
<?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