Skip to content

Commit ab38c7a

Browse files
committed
testable class
1 parent e7d55ac commit ab38c7a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/Fcm.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@
88
*/
99
class Fcm
1010
{
11+
const ENDPOINT = 'https://fcm.googleapis.com/fcm/send';
12+
1113
protected $recipients;
1214
protected $topic;
1315
protected $data;
1416
protected $notification;
1517
protected $timeToLive;
1618
protected $priority;
1719

20+
protected $serverKey;
21+
22+
public function __construct($serverKey)
23+
{
24+
$this->serverKey = $serverKey;
25+
}
26+
1827
public function to(array $recipients)
1928
{
2029
$this->recipients = $recipients;
@@ -66,8 +75,6 @@ public function timeToLive(int $timeToLive)
6675

6776
public function send()
6877
{
69-
$fcmEndpoint = 'https://fcm.googleapis.com/fcm/send';
70-
7178
$payloads = [
7279
'content_available' => true,
7380
'priority' => $this->priority ?? 'high',
@@ -85,15 +92,13 @@ public function send()
8592
$payloads['time_to_live'] = (int) $this->timeToLive;
8693
}
8794

88-
$serverKey = config('laravel-fcm.server_key');
89-
9095
$headers = [
91-
'Authorization: key=' . $serverKey,
92-
'Content-Type: application/json'
96+
'Authorization: key=' . $this->serverKey,
97+
'Content-Type: application/json',
9398
];
9499

95100
$ch = curl_init();
96-
curl_setopt($ch, CURLOPT_URL, $fcmEndpoint);
101+
curl_setopt($ch, CURLOPT_URL, self::ENDPOINT);
97102
curl_setopt($ch, CURLOPT_POST, true);
98103
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
99104
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

src/FcmServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public function boot()
2121
public function register()
2222
{
2323
$this->app->bind('fcm', function ($app) {
24-
return new Fcm();
24+
return new Fcm(
25+
config('laravel-fcm.server_key')
26+
);
2527
});
2628
}
2729
}

0 commit comments

Comments
 (0)