Browse Source

add fcm update route

mohammad
mahdihty 4 years ago
parent
commit
37a0559403
  1. 54
      app/Channels/Messages/SocketMessage.php
  2. 84
      app/Channels/SocketChannel.php
  3. 16
      app/Http/Controllers/AuthController.php
  4. 4
      app/Models/User.php
  5. 2
      routes/api.php

54
app/Channels/Messages/SocketMessage.php

@ -0,0 +1,54 @@
<?php
namespace App\Channels\Messages;
class SocketMessage
{
/**
* The devices token to send the message from.
*
* @var array|string
*/
public $to;
/**
* The data of the Socket message.
*
* @var array
*/
public $data;
/**
* Set the devices token to send the message from.
*
* @param array|string $to
* @return $this
*/
public function to($to)
{
if (is_array($to) && count($to) === 1) {
$this->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;
}
}

84
app/Channels/SocketChannel.php

@ -0,0 +1,84 @@
<?php
namespace App\Channels;
use App\Channels\Messages\FcmMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class SocketChannel
{
/**
* The API URL for Socket.
*
* @var string
*/
const API_URI = ''; //todo: fill it
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* Create a new Socket channel instance.
*
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http)
{
$this->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;
}
}

16
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 = [

4
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;

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

Loading…
Cancel
Save