Skip to content

Commit 225e2b0

Browse files
committed
Initial
0 parents  commit 225e2b0

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name" : "kawankoding/fcm",
3+
"description" : "A package to send Firebase notification cross Laravel Application",
4+
"authors" : [
5+
{
6+
"name" : "Muhammad Amirul Ihsan",
7+
"email" : "[email protected]"
8+
}
9+
],
10+
"autoload" : {
11+
"psr-4" : {
12+
"Kawankoding\\Fcm\\" : "src/"
13+
},
14+
"files" : [
15+
"src/Kawankoding/Fcm/functions.php"
16+
]
17+
}
18+
}

resources/config/laravel-fcm.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
5+
/**
6+
* Set your FCM Server Key
7+
*/
8+
9+
'server-key' => ''
10+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Kawankoding\Fcm\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* Class Fcm
9+
* @package Kawankoding\Fcm\Facades
10+
*/
11+
class Fcm extends Facade
12+
{
13+
protected static function getFacadeAccessor()
14+
{
15+
return 'fcm';
16+
}
17+
}

src/Kawankoding/Fcm/Fcm.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Kawankoding\Fcm;
4+
5+
/**
6+
* Class Fcm
7+
* @package Kawankoding\Fcm
8+
*/
9+
class Fcm
10+
{
11+
protected $recipient;
12+
protected $data;
13+
protected $notification;
14+
15+
public function to($recipient)
16+
{
17+
$this->recipient = $recipient;
18+
19+
return $this;
20+
}
21+
22+
public function data(array $data = [])
23+
{
24+
$this->data = $data;
25+
26+
return $this;
27+
}
28+
29+
public function notification(array $notification = [])
30+
{
31+
$this->notification = $notification;
32+
33+
return $this;
34+
}
35+
36+
public function push()
37+
{
38+
$fcmEndpoint = 'https://fcm.googleapis.com/fcm/send';
39+
40+
$fields = [
41+
'registration_ids' => $this->recipient,
42+
'content-available' => true,
43+
'priority' => 'high',
44+
'data' => $this->data,
45+
'notification' => $this->notification
46+
];
47+
48+
$serverKey = config('laravel-fcm.server-key');
49+
50+
$headers = [
51+
'Authorization:key='.$serverKey,
52+
'Content-Type:application/json'
53+
];
54+
55+
$ch = curl_init();
56+
curl_setopt($ch, CURLOPT_URL, $fcmEndpoint);
57+
curl_setopt($ch, CURLOPT_POST, true);
58+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
59+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CURL_IPRESOLVE_V4);
61+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
62+
$result = curl_exec($ch);
63+
64+
curl_close($ch);
65+
}
66+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Kawankoding\Fcm\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Kawankoding\Fcm\Fcm;
7+
8+
/**
9+
* Class FcmServiceProvider
10+
* @package Kawankoding\Fcm\Providers
11+
*/
12+
class FcmServiceProvider extends ServiceProvider
13+
{
14+
public function boot()
15+
{
16+
$this->publishes([
17+
__DIR__.'/../resources/config/laravel-fcm.php' => config_path('laravel-fcm.php'),
18+
]);
19+
}
20+
21+
public function register()
22+
{
23+
$this->app->singleton('fcm', function($app) {
24+
return new Fcm;
25+
});
26+
}
27+
}

src/Kawankoding/Fcm/functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
if (!function_exists('fcm')) {
4+
function fcm()
5+
{
6+
return app('fcm');
7+
}
8+
}

0 commit comments

Comments
 (0)