diff --git a/config/fcm.php b/config/fcm.php index 3257f353d..678b87f95 100755 --- a/config/fcm.php +++ b/config/fcm.php @@ -1,14 +1,14 @@ env('FCM_PROTOCOL','http'), - 'log_enabled' => true, + 'driver' => env('FCM_PROTOCOL', 'http'), + 'log_enabled' => true, - 'http' => [ - 'server_key' => env('FCM_SERVER_KEY','Your FCM server key'), - 'sender_id' => env('FCM_SENDER_ID', 'Your sender id'), - 'server_send_url' => 'https://fcm.googleapis.com/fcm/send', - 'server_group_url' => 'https://android.googleapis.com/gcm/notification', - 'timeout' => 30.0, // in second - ] + 'http' => [ + 'server_key' => env('FCM_SERVER_KEY', 'Your FCM server key'), + 'sender_id' => env('FCM_SENDER_ID', 'Your sender id'), + 'server_send_url' => 'https://fcm.googleapis.com/fcm/send', + 'server_group_url' => 'https://android.googleapis.com/gcm/notification', + 'timeout' => 30.0, // in second + ] ]; diff --git a/src/FCMManager.php b/src/FCMManager.php index 4e3c277ad..3dfdd82cf 100644 --- a/src/FCMManager.php +++ b/src/FCMManager.php @@ -1,6 +1,5 @@ app['config']['fcm.driver']; + return $this->app[ 'config' ][ 'fcm.driver' ]; } - + protected function createHttpDriver() { - $config = $this->app['config']->get('fcm.http', []); - return new Client([ - 'timeout' => $config['timeout'], - ]); + $config = $this->app[ 'config' ]->get('fcm.http', []); + + return new Client([ 'timeout' => $config[ 'timeout' ] ]); } + } \ No newline at end of file diff --git a/src/FCMServiceProvider.php b/src/FCMServiceProvider.php index 38bfe5472..de8251513 100644 --- a/src/FCMServiceProvider.php +++ b/src/FCMServiceProvider.php @@ -22,26 +22,28 @@ public function boot() public function register() { + $this->app->singleton('fcm.client', function($app) { + return (new FCMManager($app))->driver(); + }); + $this->app->bind('fcm.group', function($app) { - return new FCMGroup(); + $client = $app[ 'fcm.client' ]; + $url = $app[ 'config' ]->get('fcm.http.server_group_url'); + + return new FCMGroup($client, $url); }); $this->app->bind('fcm.sender', function($app) { - return new FCMSender(); - }); - - $this->registerClient(); - } + $client = $app[ 'fcm.client' ]; + $url = $app[ 'config' ]->get('fcm.http.server_send_url'); - public function registerClient() - { - $this->app->singleton('fcm.client', function($app) { - return (new FCMManager($app))->driver(); + return new FCMSender($client, $url); }); } protected function provide() { - return [ 'fcm.group', 'fcm.sender', 'fcm.client' ]; + return [ 'fcm.client', 'fcm.group', 'fcm.sender' ]; } + } \ No newline at end of file diff --git a/src/Sender/BaseSender.php b/src/Sender/BaseSender.php deleted file mode 100644 index ed412bc0d..000000000 --- a/src/Sender/BaseSender.php +++ /dev/null @@ -1,45 +0,0 @@ -client = app('fcm.client'); - $this->config = app('config')->get('fcm.http', []); - - $this->url = $this->getUrl(); - } - - /** - * get the url - * - * @return string - */ - protected abstract function getUrl(); -} \ No newline at end of file diff --git a/src/Sender/FCMGroup.php b/src/Sender/FCMGroup.php index 69579076a..cbfad6632 100644 --- a/src/Sender/FCMGroup.php +++ b/src/Sender/FCMGroup.php @@ -8,7 +8,7 @@ * * @package LaravelFCM\Sender */ -class FCMGroup extends BaseSender { +class FCMGroup extends HTTPSender { const CREATE = "create"; const ADD = "add"; @@ -96,13 +96,4 @@ public function isValidResponse(GuzzleResponse $response) return $response->getReasonPhrase() != 'OK' || $response->getStatusCode() != 200; } - /** - * get the url - * - * @return string - */ - protected function getUrl() - { - return $this->config['server_group_url']; - } } \ No newline at end of file diff --git a/src/Sender/FCMSender.php b/src/Sender/FCMSender.php index bdcbe36dd..78e744e24 100644 --- a/src/Sender/FCMSender.php +++ b/src/Sender/FCMSender.php @@ -15,7 +15,7 @@ * * @package LaravelFCM\Sender */ -class FCMSender extends BaseSender { +class FCMSender extends HTTPSender { const MAX_TOKEN_PER_REQUEST = 1000; @@ -37,7 +37,6 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi { $response = null; - if (is_array($to) && !empty($to)) { $partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false); @@ -96,7 +95,6 @@ public function sendToGroup($notificationKey, Options $options = null, PayloadNo */ public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null) { - $request = new Request(null, $options, $notification, $data, $topics); $responseGuzzle = $this->post($request); @@ -104,17 +102,6 @@ public function sendToTopic(Topics $topics, Options $options = null, PayloadNoti return new TopicResponse($responseGuzzle, $topics); } - - /** - * get the url - * - * @return string - */ - protected function getUrl() - { - return $this->config[ 'server_send_url' ]; - } - /** * @internal * diff --git a/src/Sender/HTTPSender.php b/src/Sender/HTTPSender.php new file mode 100644 index 000000000..43b419ced --- /dev/null +++ b/src/Sender/HTTPSender.php @@ -0,0 +1,38 @@ +client = $client; + $this->url = $url; + } + +} \ No newline at end of file diff --git a/tests/DownstreamTest.php b/tests/DownstreamTest.php index 5e2039890..d6f905815 100644 --- a/tests/DownstreamTest.php +++ b/tests/DownstreamTest.php @@ -23,13 +23,10 @@ public function it_send_a_notification_to_a_device() $client = Mockery::mock(Client::class); $client->shouldReceive('post')->once()->andReturn($response); - $this->app->singleton('fcm.client', function($app) use($client) { - return $client; - }); $tokens = 'uniqueToken'; - $fcm = new FCMSender(); + $fcm = new FCMSender($client, 'http://test.test'); $fcm->sendTo($tokens); } @@ -56,16 +53,13 @@ public function it_send_a_notification_to_more_than_1000_devices() $client = Mockery::mock(Client::class); $client->shouldReceive('post')->times(10)->andReturn($response); - $this->app->singleton('fcm.client', function($app) use($client) { - return $client; - }); $tokens = []; for ($i=0 ; $i<10000 ; $i++) { $tokens[$i] = 'token_'.$i; } - $fcm = new FCMSender(); + $fcm = new FCMSender($client, 'http://test.test'); $fcm->sendTo($tokens); } @@ -91,11 +85,8 @@ public function an_empty_array_of_tokens_thrown_an_exception() $client = Mockery::mock(Client::class); $client->shouldReceive('post')->once()->andReturn($response); - $this->app->singleton('fcm.client', function($app) use($client) { - return $client; - }); - $fcm = new FCMSender(); + $fcm = new FCMSender($client, 'http://test.test'); $this->setExpectedException(\LaravelFCM\Response\Exceptions\InvalidRequestException::class); $fcm->sendTo([]); } diff --git a/tests/FCMTestCase.php b/tests/FCMTestCase.php index 12eadcbc8..a912fe234 100644 --- a/tests/FCMTestCase.php +++ b/tests/FCMTestCase.php @@ -1,13 +1,16 @@ make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $app->register(LaravelFCM\FCMServiceProvider::class); + $app['config']['fcm.driver'] = 'http'; $app['config']['fcm.http.timeout'] = 20; $app['config']['fcm.http.server_send_url'] = 'http://test.test'; @@ -16,4 +19,5 @@ public function createApplication() return $app; } + } \ No newline at end of file diff --git a/tests/GroupResponseTest.php b/tests/GroupResponseTest.php index e951ce724..26b67357e 100644 --- a/tests/GroupResponseTest.php +++ b/tests/GroupResponseTest.php @@ -9,7 +9,6 @@ class GroupResponseTest extends FCMTestCase { */ public function it_construct_a_response_with_successes() { - $notificationKey = "notificationKey"; $response = new \GuzzleHttp\Psr7\Response(200, [], '{ @@ -29,7 +28,6 @@ public function it_construct_a_response_with_successes() */ public function it_construct_a_response_with_failures() { - $notificationKey = "notificationKey"; $response = new \GuzzleHttp\Psr7\Response(200, [], '{ @@ -55,7 +53,6 @@ public function it_construct_a_response_with_failures() */ public function it_construct_a_response_with_partials_failures() { - $notificationKey = "notificationKey"; $response = new \GuzzleHttp\Psr7\Response(200, [], '{ diff --git a/tests/TopicsTest.php b/tests/TopicsTest.php index b6a267357..33765b3b1 100644 --- a/tests/TopicsTest.php +++ b/tests/TopicsTest.php @@ -17,7 +17,6 @@ public function it_throw_an_exception_if_no_topic_is_provided() $this->setExpectedException(NoTopicProvidedException::class); $topics->build(); - } /** @@ -32,7 +31,6 @@ public function it_has_only_one_topic() $topics->topic('myTopic'); $this->assertEquals($target, $topics->build()); - } /** @@ -115,12 +113,8 @@ public function it_send_a_notification_to_a_topic() $client = Mockery::mock(Client::class); $client->shouldReceive('post')->once()->andReturn($response); - $this->app->singleton('fcm.client', function($app) use($client) { - return $client; - }); - - $fcm = new FCMSender(); + $fcm = new FCMSender($client, 'http://test.test'); $topics = new Topics(); $topics->topic('test'); @@ -141,12 +135,8 @@ public function it_send_a_notification_to_a_topic_and_return_error() $client = Mockery::mock(Client::class); $client->shouldReceive('post')->once()->andReturn($response); - $this->app->singleton('fcm.client', function($app) use($client) { - return $client; - }); - - $fcm = new FCMSender(); + $fcm = new FCMSender($client, 'http://test.test'); $topics = new Topics(); $topics->topic('test');