Last active
August 29, 2015 14:02
-
-
Save crynobone/56a6385863be221bfd88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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