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) {