Browse Source

Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad

mohammad
Mohammad Akbari 4 years ago
parent
commit
6e5df4f9df
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 50
      app/Channels/Messages/SocketMessage.php
  2. 61
      app/Channels/SocketChannel.php
  3. 16
      app/Http/Controllers/AuthController.php
  4. 14
      app/Models/User.php
  5. 2
      app/Notifications/FcmNotification.php
  6. 52
      app/Notifications/SocketNotification.php
  7. 4
      app/Providers/AppServiceProvider.php
  8. 9
      app/Utilities/HelperClass/NotificationHelper.php
  9. 18
      config/socket.php
  10. 6
      routes/api.php

50
app/Channels/Messages/SocketMessage.php

@ -0,0 +1,50 @@
<?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)
{
$this->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;
}
}

61
app/Channels/SocketChannel.php

@ -0,0 +1,61 @@
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class SocketChannel
{
/**
* The API URL for Socket.
*
* @var string
*/
protected $socket_url;
/**
* 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, string $socket_url)
{
$this->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],
]);
}
}

16
app/Http/Controllers/AuthController.php

@ -249,6 +249,22 @@ class AuthController extends Controller
return 'success'; 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() public function createFingerPrint()
{ {
$attributes = [ $attributes = [

14
app/Models/User.php

@ -2,10 +2,6 @@
namespace App\Models; 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\Notifications\Notifiable;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Illuminate\Http\UploadedFile; 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(); 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() public function updateRelations()
{ {
// projects relations // projects relations

2
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 * @param mixed $notifiable
* @return FcmMessage * @return FcmMessage

52
app/Notifications/SocketNotification.php

@ -0,0 +1,52 @@
<?php
namespace App\Notifications;
use App\Channels\Messages\SocketMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class SocketNotification extends Notification
{
use Queueable;
public $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message)
{
$this->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'],
]);
}
}

4
app/Providers/AppServiceProvider.php

@ -3,6 +3,7 @@
namespace App\Providers; namespace App\Providers;
use App\Channels\FcmChannel; use App\Channels\FcmChannel;
use App\Channels\SocketChannel;
use Illuminate\Notifications\ChannelManager; use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -21,6 +22,9 @@ class AppServiceProvider extends ServiceProvider
$service->extend('fcm', function ($app) { $service->extend('fcm', function ($app) {
return new FcmChannel(new HttpClient, config('fcm.key')); return new FcmChannel(new HttpClient, config('fcm.key'));
}); });
$service->extend('socket', function ($app) {
return new SocketChannel(new HttpClient, config('socket.url'));
});
}); });
} }

9
app/Utilities/HelperClass/NotificationHelper.php

@ -62,14 +62,11 @@ class NotificationHelper
case "medium": case "medium":
Notification::send($users, new FcmNotification($notif)); Notification::send($users, new FcmNotification($notif));
case "low": case "low":
// Notification::send($users, new SocketNotification($notif));
Notification::send($users, new SocketNotification($notif));
break;
default: 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));
} }
} }

18
config/socket.php

@ -0,0 +1,18 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| FCM API Key
|--------------------------------------------------------------------------
|
| This key allows you to send Push Notifications. To obtain this key go
| to the porject settings and click on the "Cloud Messaging" tab, now
| copy the API Key of "Legacy server key".
|
*/
'url' => env('SOCKET_URL'),
];

6
routes/api.php

@ -2,6 +2,10 @@
/** @var \Illuminate\Routing\$router */ /** @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' => 'actions'], function () use ($router) {
$router->group(['prefix' => 'businesses'], function () use ($router) { $router->group(['prefix' => 'businesses'], function () use ($router) {
$router->group(['prefix' => '{business}', 'middleware' => 'bindBusiness'], 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/redirect', 'AuthController@redirectToGoogle')->name('google.redirect');
$router->get('google/callback', 'AuthController@handleGoogleCallback')->name('google.callback'); $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) { $router->group(['prefix' => 'businesses', 'middleware' => 'auth:api'], function () use ($router) {

Loading…
Cancel
Save