Skip to content

Commit

Permalink
testable class
Browse files Browse the repository at this point in the history
  • Loading branch information
erlangparasu committed Feb 29, 2020
1 parent e7d55ac commit ab38c7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/Fcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
*/
class Fcm
{
const ENDPOINT = 'https://fcm.googleapis.com/fcm/send';

protected $recipients;
protected $topic;
protected $data;
protected $notification;
protected $timeToLive;
protected $priority;

protected $serverKey;

public function __construct($serverKey)
{
$this->serverKey = $serverKey;
}

public function to(array $recipients)
{
$this->recipients = $recipients;
Expand Down Expand Up @@ -66,8 +75,6 @@ public function timeToLive(int $timeToLive)

public function send()
{
$fcmEndpoint = 'https://fcm.googleapis.com/fcm/send';

$payloads = [
'content_available' => true,
'priority' => $this->priority ?? 'high',
Expand All @@ -85,15 +92,13 @@ public function send()
$payloads['time_to_live'] = (int) $this->timeToLive;
}

$serverKey = config('laravel-fcm.server_key');

$headers = [
'Authorization: key=' . $serverKey,
'Content-Type: application/json'
'Authorization: key=' . $this->serverKey,
'Content-Type: application/json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fcmEndpoint);
curl_setopt($ch, CURLOPT_URL, self::ENDPOINT);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand Down
4 changes: 3 additions & 1 deletion src/FcmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public function boot()
public function register()
{
$this->app->bind('fcm', function ($app) {
return new Fcm();
return new Fcm(
config('laravel-fcm.server_key')
);
});
}
}

0 comments on commit ab38c7a

Please sign in to comment.