Browse Source
Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad
mohammad
Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad
mohammad
Mohammad Akbari
4 years ago
10 changed files with 221 additions and 11 deletions
-
50app/Channels/Messages/SocketMessage.php
-
61app/Channels/SocketChannel.php
-
16app/Http/Controllers/AuthController.php
-
14app/Models/User.php
-
2app/Notifications/FcmNotification.php
-
52app/Notifications/SocketNotification.php
-
4app/Providers/AppServiceProvider.php
-
9app/Utilities/HelperClass/NotificationHelper.php
-
18config/socket.php
-
6routes/api.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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -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], |
||||
|
]); |
||||
|
} |
||||
|
} |
@ -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'], |
||||
|
]); |
||||
|
} |
||||
|
} |
@ -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'), |
||||
|
|
||||
|
]; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue