Skip to content

Commit ce8fca4

Browse files
authored
Merge pull request #9 from pinguinjkeke/master
Fix multiple messages issue (#6)
2 parents 3af0682 + 60f2409 commit ce8fca4

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
class CouldNotSendNotification extends \Exception
66
{
7+
/**
8+
* Create service exception.
9+
*
10+
* @param $response
11+
* @return static
12+
*/
713
public static function serviceRespondedWithAnError($response)
814
{
915
return new static('Descriptive error message.');

src/FirebaseChannel.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,68 @@
66
use paragraph1\phpFCM\Client;
77
use paragraph1\phpFCM\Message;
88
use paragraph1\phpFCM\Recipient\Device;
9+
use \Illuminate\Events\Dispatcher;
10+
use Illuminate\Notifications\Events\NotificationFailed;
911
use Illuminate\Notifications\Notification;
1012

1113
class FirebaseChannel
1214
{
1315
/**
14-
* @var paragraph1\phpFCM\Client
16+
* FCM client.
17+
*
18+
* @var \paragraph1\phpFCM\Client
1519
*/
1620
protected $client;
1721

1822
/**
19-
* @var paragraph1\phpFCM\Message
23+
* Events dispatcher.
24+
*
25+
* @var \Illuminate\Events\Dispatcher
2026
*/
21-
protected $message;
27+
protected $events;
2228

2329
/**
2430
* Push Service constructor.
2531
*
26-
* @param paragraph1\phpFCM\Client $client
27-
* @param paragraph1\phpFCM\Message $message
32+
* @param \paragraph1\phpFCM\Client $client
33+
* @param \Illuminate\Events\Dispatcher $events
2834
*/
29-
public function __construct(Client $client, Message $message)
35+
public function __construct(Client $client, Dispatcher $events)
3036
{
3137
$this->client = $client;
32-
$this->message = $message;
38+
$this->events = $events;
3339
}
3440

3541
/**
3642
* Send the given notification.
3743
*
38-
* @param mixed $notifiable
39-
* @param \Illuminate\Notifications\Notification $notification
44+
* @param mixed $notifiable
45+
* @param \Illuminate\Notifications\Notification $notification
4046
* @return void
4147
*/
4248
public function send($notifiable, Notification $notification)
4349
{
4450
$devices = $notifiable->routeNotificationFor('firebase');
51+
4552
if (empty($devices)) {
4653
return;
4754
}
4855

4956
$firebase = $notification->toFirebase($notifiable);
5057

51-
foreach ($devices as $device) {
52-
$this->message->addRecipient(new Device($device));
53-
}
54-
55-
$this->message->setNotification($firebase->notification)->setData($firebase->data);
56-
5758
try {
5859
foreach ($devices as $device) {
59-
$response = $this->client->send($this->message);
60+
$message = (new Message())
61+
->addRecipient(new Device($device))
62+
->setNotification($firebase->notification)
63+
->setData($firebase->data);
64+
65+
$this->client->send($message);
6066
}
6167
} catch (Exception $e) {
68+
$this->events->fire(
69+
new NotificationFailed($notifiable, $notification, $this)
70+
);
6271
}
6372
}
6473
}

src/FirebaseMessage.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66

77
class FirebaseMessage
88
{
9+
/**
10+
* Notification.
11+
*
12+
* @var \paragraph1\phpFCM\Notification
13+
*/
914
public $notification;
1015

16+
/**
17+
* Custom data.
18+
*
19+
* @var array
20+
*/
1121
public $data = [];
1222

1323
/**
14-
* @param string $body
24+
* Static constructor.
1525
*
26+
* @param string $title
27+
* @param string $body
1628
* @return static
1729
*/
1830
public static function create($title = '', $body = '')
@@ -21,6 +33,9 @@ public static function create($title = '', $body = '')
2133
}
2234

2335
/**
36+
* FirebaseMessage constructor.
37+
*
38+
* @param string $title
2439
* @param string $body
2540
*/
2641
public function __construct($title = '', $body = '')
@@ -35,7 +50,6 @@ public function __construct($title = '', $body = '')
3550
* Set the message body.
3651
*
3752
* @param string $value
38-
*
3953
* @return $this
4054
*/
4155
public function body($value)
@@ -49,7 +63,6 @@ public function body($value)
4963
* Set the message title.
5064
*
5165
* @param string $value
52-
*
5366
* @return $this
5467
*/
5568
public function title($value)
@@ -63,7 +76,6 @@ public function title($value)
6376
* Set notification sound.
6477
*
6578
* @param string $value
66-
*
6779
* @return $this
6880
*/
6981
public function sound($value = 'default')
@@ -77,7 +89,6 @@ public function sound($value = 'default')
7789
* Set badge number. IOS only.
7890
*
7991
* @param int $integer
80-
*
8192
* @return $this
8293
*/
8394
public function badge($integer)

src/FirebaseServiceProvider.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace Alfa6661\Firebase;
44

55
use Exception;
6-
use paragraph1\phpFCM\Client;
6+
use GuzzleHttp\Client as GuzzleClient;
7+
use paragraph1\phpFCM\Client as FcmClient;
78
use Illuminate\Support\ServiceProvider;
89

910
class FirebaseServiceProvider extends ServiceProvider
@@ -14,24 +15,19 @@ class FirebaseServiceProvider extends ServiceProvider
1415
public function boot()
1516
{
1617
$this->app->when(FirebaseChannel::class)
17-
->needs(Client::class)
18+
->needs(FcmClient::class)
1819
->give(function () {
1920
$firebaseConfig = config('services.firebase');
21+
2022
if (is_null($firebaseConfig)) {
2123
throw new Exception('In order to send notification via firebase you need to add credentials in the `firebase` key of `config.services`.');
2224
}
23-
$client = new Client();
25+
26+
$client = new FcmClient();
2427
$client->setApiKey($firebaseConfig['api_key']);
25-
$client->injectHttpClient(new \GuzzleHttp\Client());
28+
$client->injectHttpClient(new GuzzleClient());
2629

2730
return $client;
2831
});
2932
}
30-
31-
/**
32-
* Register the application services.
33-
*/
34-
public function register()
35-
{
36-
}
3733
}

0 commit comments

Comments
 (0)