From 37a0559403c34e070c3188c1dbc0566b1569824e Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sat, 13 Mar 2021 17:48:03 +0330 Subject: [PATCH] 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) {