88 */
99class 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 );
0 commit comments