Created
November 20, 2017 16:41
-
-
Save juanmanavarro/8d98766bd6c8b327d00178522ed5aa3b to your computer and use it in GitHub Desktop.
FCM push library
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 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