Skip to content

Instantly share code, notes, and snippets.

@juanmanavarro
Created November 20, 2017 16:41
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 juanmanavarro/8d98766bd6c8b327d00178522ed5aa3b to your computer and use it in GitHub Desktop.
Save juanmanavarro/8d98766bd6c8b327d00178522ed5aa3b to your computer and use it in GitHub Desktop.
FCM push library
<?php namespace App\Libraries;
use GuzzleHttp\Client;
class PushLibrary {
protected $users;
protected $title;
protected $data;
public static function factory($title, $data, $users)
{
return new PushLibrary($title, $data, $users);
}
public function __construct($title, $data, $users)
{
$this->title = $title;
$this->data = $data;
$this->users = $users;
}
public function send()
{
$client = new Client();
$this->data['title'] = $this->title;
$registration_ids = collect($this->users)->lists('device_token');
$data = [
'data' => $this->data->toArray(),
'registration_ids' => $registration_ids,
];
$response = $client->post(config('push.url'), [
'json' => $data,
'headers' => [
'Authorization' => 'key=' . env('FCM_API_KEY'),
'Content-Type' => 'application/json'
],
]);
$response = $response->json();
if($response['failure']) {
\Log::error('push_error', $response);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment