From 37a0559403c34e070c3188c1dbc0566b1569824e Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sat, 13 Mar 2021 17:48:03 +0330 Subject: [PATCH 1/5] add fcm update route --- app/Channels/Messages/SocketMessage.php | 54 ++++++++++++++++ app/Channels/SocketChannel.php | 84 +++++++++++++++++++++++++ app/Http/Controllers/AuthController.php | 16 +++++ app/Models/User.php | 4 -- routes/api.php | 2 + 5 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 app/Channels/Messages/SocketMessage.php create mode 100644 app/Channels/SocketChannel.php diff --git a/app/Channels/Messages/SocketMessage.php b/app/Channels/Messages/SocketMessage.php new file mode 100644 index 0000000..a2e4758 --- /dev/null +++ b/app/Channels/Messages/SocketMessage.php @@ -0,0 +1,54 @@ +to = $to[0]; + } else { + $this->to = $to; + } + + return $this; + } + + /** + * Set the data of the FCM 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..5eb5eba --- /dev/null +++ b/app/Channels/SocketChannel.php @@ -0,0 +1,84 @@ +http = $http; + } + + /** + * 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(self::API_URI, [ + 'headers' => [ + 'Authorization' => "key={$this->apiKey}", + 'Content-Type' => 'application/json', + ], + 'json' => $this->buildJsonPayload($message), + ]); + } + + 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/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..016aebf 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; diff --git a/routes/api.php b/routes/api.php index be7bd04..dce2886 100644 --- a/routes/api.php +++ b/routes/api.php @@ -29,6 +29,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) { From 30430b27e06176d6f624aa344fecb246e62469b3 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sat, 13 Mar 2021 19:27:39 +0330 Subject: [PATCH 2/5] add socket channel, message, notification and some minor change --- app/Channels/Messages/SocketMessage.php | 8 +--- app/Channels/SocketChannel.php | 34 +++------------- app/Notifications/FcmNotification.php | 2 +- app/Notifications/SocketNotification.php | 52 ++++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 4 ++ config/socket.php | 18 ++++++++ routes/api.php | 4 ++ 7 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 app/Notifications/SocketNotification.php create mode 100644 config/socket.php 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) { From d264fe0bc9890999a5ba21a6a4fbc182b82f9f1c Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sun, 14 Mar 2021 12:22:16 +0330 Subject: [PATCH 3/5] complete socket notification channel --- app/Channels/SocketChannel.php | 3 +-- app/Models/User.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/Channels/SocketChannel.php b/app/Channels/SocketChannel.php index 51af1a8..967475c 100644 --- a/app/Channels/SocketChannel.php +++ b/app/Channels/SocketChannel.php @@ -46,7 +46,6 @@ class SocketChannel $message = $notification->toSocket($notifiable); $message->to($notifiable->routeNotificationFor('socket', $notification)); - $message->to('1'); if (! $message->to) { return; @@ -56,7 +55,7 @@ class SocketChannel 'headers' => [ 'Content-Type' => 'application/json', ], - 'data' => [$message->data], + 'json' => ['data' => $message->data], ]); } } diff --git a/app/Models/User.php b/app/Models/User.php index 016aebf..ea04af2 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -50,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 From a73846eb1daa915a0ff2bd983a2db6eef34b7b0f Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sun, 14 Mar 2021 12:22:21 +0330 Subject: [PATCH 4/5] complete socket notification channel --- routes/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index 3e524db..03620f7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -5,7 +5,7 @@ $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) { From a0a41a57569e4030b6d4dcfe37ec70ee2e30e452 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sun, 14 Mar 2021 13:55:16 +0330 Subject: [PATCH 5/5] some change in notification helper --- app/Utilities/HelperClass/NotificationHelper.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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)); } }