diff --git a/app/Channels/Messages/SocketMessage.php b/app/Channels/Messages/SocketMessage.php new file mode 100644 index 0000000..2154740 --- /dev/null +++ b/app/Channels/Messages/SocketMessage.php @@ -0,0 +1,50 @@ +to = $to; + + return $this; + } + + /** + * Set the data of the socket message. + * + * @param array $data + * @return $this + */ + public function data(array $data) + { + $this->data = $data; + + return $this; + } + +} diff --git a/app/Channels/SocketChannel.php b/app/Channels/SocketChannel.php new file mode 100644 index 0000000..967475c --- /dev/null +++ b/app/Channels/SocketChannel.php @@ -0,0 +1,61 @@ +http = $http; + $this->socket_url = $socket_url; + } + + /** + * Send the given notification. + * + * @param mixed $notifiable + * @param \Illuminate\Notifications\Notification $notification + * @return void + */ + public function send($notifiable, Notification $notification) + { + $message = $notification->toSocket($notifiable); + + $message->to($notifiable->routeNotificationFor('socket', $notification)); + + if (! $message->to) { + return; + } + + $this->http->post($this->socket_url . '/emit/' . $message->to, [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'json' => ['data' => $message->data], + ]); + } +} diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 55fe6f7..1051350 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -249,6 +249,22 @@ class AuthController extends Controller return 'success'; } + public function updateFcmToken(Request $request) + { + Auth::user()->fingerprints()->where( + [ + ['agent', request()->getAgent()], + ['ip', request()->getClientIp()], + ['os', request()->getOS()], + ['latitude', \request()->getLocation()->getAttribute('lat')], + ['longitude', \request()->getLocation()->getAttribute('lon')], + ] + )->firstOrFail()->update([ + 'fcm_token' => $request->fcm_token + ]); + return $this->authWithInfo(); + } + public function createFingerPrint() { $attributes = [ diff --git a/app/Models/User.php b/app/Models/User.php index 5095cb9..ea04af2 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,10 +2,6 @@ namespace App\Models; -use App\Models\File; -use App\Models\Model; -use App\Models\SoftDeletes; -use App\Models\ReportableRelation; use Illuminate\Notifications\Notifiable; use Illuminate\Validation\Rule; use Illuminate\Http\UploadedFile; @@ -54,6 +50,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac return $this->fingerprints->whereNotNull('fcm_token')->pluck('fcm_token')->all(); } + /** + * Specifies the user's Socket room + * + * @return string + */ + public function routeNotificationForSocket() + { + return request('_business_info')['id'] ?? null; + } + public function updateRelations() { // projects relations 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/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php index 582989b..87b61b9 100644 --- a/app/Utilities/HelperClass/NotificationHelper.php +++ b/app/Utilities/HelperClass/NotificationHelper.php @@ -62,14 +62,11 @@ class NotificationHelper case "medium": Notification::send($users, new FcmNotification($notif)); case "low": -// Notification::send($users, new SocketNotification($notif)); + Notification::send($users, new SocketNotification($notif)); + break; default: -// Notification::send($users, new SocketNotification($notif)); + Notification::send($users, new SocketNotification($notif)); } - -// Notification::send($users, new MailNotification($this->notif)); -// Notification::send($users, new DBNotification($this->notif)); -// Notification::send($users, new FcmNotification($this->notif)); } } 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 be7bd04..03620f7 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'])); +})->middleware('bindBusiness'); $router->group(['prefix' => 'actions'], function () use ($router) { $router->group(['prefix' => 'businesses'], function () use ($router) { $router->group(['prefix' => '{business}', 'middleware' => 'bindBusiness'], function () use ($router) { @@ -29,6 +33,8 @@ $router->group(['prefix' => 'auth'], function () use ($router) { $router->get('google/redirect', 'AuthController@redirectToGoogle')->name('google.redirect'); $router->get('google/callback', 'AuthController@handleGoogleCallback')->name('google.callback'); + + $router->get('update-fcm', 'AuthController@updateFcmToken')->middleware('auth:api'); }); $router->group(['prefix' => 'businesses', 'middleware' => 'auth:api'], function () use ($router) {