diff --git a/app/Channels/Messages/SocketMessage.php b/app/Channels/Messages/SocketMessage.php index a2e4758..2154740 100644 --- a/app/Channels/Messages/SocketMessage.php +++ b/app/Channels/Messages/SocketMessage.php @@ -29,17 +29,13 @@ class SocketMessage */ public function to($to) { - if (is_array($to) && count($to) === 1) { - $this->to = $to[0]; - } else { - $this->to = $to; - } + $this->to = $to; return $this; } /** - * Set the data of the FCM message. + * Set the data of the socket message. * * @param array $data * @return $this diff --git a/app/Channels/SocketChannel.php b/app/Channels/SocketChannel.php index 5eb5eba..51af1a8 100644 --- a/app/Channels/SocketChannel.php +++ b/app/Channels/SocketChannel.php @@ -3,7 +3,6 @@ namespace App\Channels; -use App\Channels\Messages\FcmMessage; use Illuminate\Notifications\Notification; use GuzzleHttp\Client as HttpClient; @@ -14,7 +13,7 @@ class SocketChannel * * @var string */ - const API_URI = ''; //todo: fill it + protected $socket_url; /** * The HTTP client instance. @@ -29,9 +28,10 @@ class SocketChannel * @param \GuzzleHttp\Client $http * @return void */ - public function __construct(HttpClient $http) + public function __construct(HttpClient $http, string $socket_url) { $this->http = $http; + $this->socket_url = $socket_url; } /** @@ -46,39 +46,17 @@ class SocketChannel $message = $notification->toSocket($notifiable); $message->to($notifiable->routeNotificationFor('socket', $notification)); + $message->to('1'); if (! $message->to) { return; } - $this->http->post(self::API_URI, [ + $this->http->post($this->socket_url . '/emit/' . $message->to, [ 'headers' => [ - 'Authorization' => "key={$this->apiKey}", 'Content-Type' => 'application/json', ], - 'json' => $this->buildJsonPayload($message), + 'data' => [$message->data], ]); } - - protected function buildJsonPayload(FcmMessage $message) - { - $payload = array_filter([ - 'priority' => $message->priority, - 'data' => $message->data, - 'notification' => $message->notification, - 'condition' => $message->condition, - ]); - - if ($message->topic) { - $payload['to'] = "/topics/{$message->topic}"; - } else { - if (is_array($message->to)) { - $payload['registration_ids'] = $message->to; - } else { - $payload['to'] = $message->to; - } - } - - return $payload; - } } diff --git a/app/Notifications/FcmNotification.php b/app/Notifications/FcmNotification.php index d9f1556..0c51e59 100644 --- a/app/Notifications/FcmNotification.php +++ b/app/Notifications/FcmNotification.php @@ -34,7 +34,7 @@ class FcmNotification extends Notification } /** - * Get the voice representation of the notification. + * Get the fcm representation of the notification. * * @param mixed $notifiable * @return FcmMessage diff --git a/app/Notifications/SocketNotification.php b/app/Notifications/SocketNotification.php new file mode 100644 index 0000000..a734ba9 --- /dev/null +++ b/app/Notifications/SocketNotification.php @@ -0,0 +1,52 @@ +message = $message; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return ['socket']; + } + + /** + * Get the socket representation of the notification. + * + * @param mixed $notifiable + * @return SocketMessage + */ + public function toSocket($notifiable) + { + return (new SocketMessage()) + ->data([ + 'title' => $this->message['title'], + 'body' => $this->message['body'], + ]); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 2dae397..281bc7c 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use App\Channels\FcmChannel; +use App\Channels\SocketChannel; use Illuminate\Notifications\ChannelManager; use Illuminate\Support\Facades\Notification; use Illuminate\Support\ServiceProvider; @@ -21,6 +22,9 @@ class AppServiceProvider extends ServiceProvider $service->extend('fcm', function ($app) { return new FcmChannel(new HttpClient, config('fcm.key')); }); + $service->extend('socket', function ($app) { + return new SocketChannel(new HttpClient, config('socket.url')); + }); }); } diff --git a/config/socket.php b/config/socket.php new file mode 100644 index 0000000..342cc06 --- /dev/null +++ b/config/socket.php @@ -0,0 +1,18 @@ + env('SOCKET_URL'), + +]; diff --git a/routes/api.php b/routes/api.php index dce2886..3e524db 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,10 @@ /** @var \Illuminate\Routing\$router */ +$router->get('/ntest', function () { + $user = \App\Models\User::find(1); + \Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss'])); +}); $router->group(['prefix' => 'actions'], function () use ($router) { $router->group(['prefix' => 'businesses'], function () use ($router) { $router->group(['prefix' => '{business}', 'middleware' => 'bindBusiness'], function () use ($router) {