14 Commits
235babe541
...
f13cdf4d46
23 changed files with 2731 additions and 82 deletions
-
2.env.example
-
45app/Channels/DBChannel.php
-
50app/Channels/Messages/SocketMessage.php
-
66app/Channels/SocketChannel.php
-
16app/Http/Controllers/AuthController.php
-
23app/Http/Controllers/NotificationController.php
-
5app/Listeners/BusinessUserCreateNotif.php
-
2app/Listeners/ProjectUserCreateNotif.php
-
14app/Models/User.php
-
18app/Notifications/DBNotification.php
-
3app/Notifications/FcmNotification.php
-
63app/Notifications/SmsNotification.php
-
52app/Notifications/SocketNotification.php
-
8app/Providers/AppServiceProvider.php
-
19app/Utilities/HelperClass/NotificationHelper.php
-
3composer.json
-
299composer.lock
-
63config/logging.php
-
18config/socket.php
-
1database/migrations/2021_03_08_114700_create_notifications_table.php
-
40docker-compose.yml
-
1990liwo.json
-
13routes/api.php
@ -0,0 +1,45 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Channels; |
|||
|
|||
use Illuminate\Notifications\Channels\DatabaseChannel; |
|||
use Illuminate\Notifications\Notification; |
|||
|
|||
class DBChannel extends DatabaseChannel |
|||
{ |
|||
|
|||
/** |
|||
* Get the data for the notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @param \Illuminate\Notifications\Notification $notification |
|||
* @return array |
|||
* |
|||
* @throws \RuntimeException |
|||
*/ |
|||
protected function getOptionalData($notifiable, Notification $notification) |
|||
{ |
|||
if (method_exists($notification, 'toDatabaseOptional')) { |
|||
return is_array($data = $notification->toDatabaseOptional($notifiable)) |
|||
? $data : [$data]; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Build an array payload for the DatabaseNotification Model. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @param \Illuminate\Notifications\Notification $notification |
|||
* @return array |
|||
*/ |
|||
protected function buildPayload($notifiable, Notification $notification) |
|||
{ |
|||
return [ |
|||
'id' => $notification->id, |
|||
'type' => get_class($notification), |
|||
'data' => $this->getData($notifiable, $notification), |
|||
'read_at' => null, |
|||
] + $this->getOptionalData($notifiable, $notification); |
|||
} |
|||
} |
@ -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,66 @@ |
|||
<?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; |
|||
} |
|||
|
|||
try { |
|||
$this->http->post($this->socket_url . '/emit/' . $message->to, [ |
|||
'headers' => [ |
|||
'Content-Type' => 'application/json', |
|||
], |
|||
'json' => ['data' => $message->data], |
|||
]); |
|||
} catch (\GuzzleHttp\Exception\ConnectException $e) { |
|||
report($e); |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers; |
|||
|
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\Auth; |
|||
|
|||
class NotificationController extends Controller |
|||
{ |
|||
public function index($business) |
|||
{ |
|||
return Auth::user()->notifications() |
|||
->where('data->business_id', $business)->get(); |
|||
} |
|||
|
|||
public function markAsRead($business, $notification) |
|||
{ |
|||
return Auth::user()->unreadNotifications() |
|||
->where('business_id', $business) |
|||
->findOrFail($notification) |
|||
->markAsRead(); |
|||
} |
|||
} |
@ -0,0 +1,63 @@ |
|||
<?php |
|||
|
|||
namespace App\Notifications; |
|||
|
|||
use Illuminate\Bus\Queueable; |
|||
use Illuminate\Contracts\Queue\ShouldQueue; |
|||
use Illuminate\Notifications\Messages\MailMessage; |
|||
use Illuminate\Notifications\Notification; |
|||
|
|||
class SmsNotification 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 ['mail']; |
|||
} |
|||
|
|||
/** |
|||
* Get the mail representation of the notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @return \Illuminate\Notifications\Messages\MailMessage |
|||
*/ |
|||
public function toMail($notifiable) |
|||
{ |
|||
return (new MailMessage) |
|||
->line('The introduction to the notification.') |
|||
->action('Notification Action', url('/')) |
|||
->line('Thank you for using our application!'); |
|||
} |
|||
|
|||
/** |
|||
* Get the array representation of the notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @return array |
|||
*/ |
|||
public function toArray($notifiable) |
|||
{ |
|||
return [ |
|||
//
|
|||
]; |
|||
} |
|||
} |
@ -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'), |
|||
|
|||
]; |
1990
liwo.json
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue