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
16 changed files with 490 additions and 76 deletions
-
3.env.example
-
45app/Channels/DBChannel.php
-
68app/Channels/SmsChannel.php
-
5app/Channels/SocketChannel.php
-
23app/Http/Controllers/NotificationController.php
-
5app/Listeners/BusinessUserCreateNotif.php
-
2app/Listeners/ProjectUserCreateNotif.php
-
18app/Notifications/DBNotification.php
-
1app/Notifications/FcmNotification.php
-
58app/Notifications/SmsNotification.php
-
4app/Providers/AppServiceProvider.php
-
14app/Utilities/HelperClass/NotificationHelper.php
-
299composer.lock
-
1database/migrations/2021_03_08_114700_create_notifications_table.php
-
1docker-compose.yml
-
7routes/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,68 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Channels; |
|||
|
|||
use Illuminate\Notifications\Notification; |
|||
use GuzzleHttp\Client as HttpClient; |
|||
|
|||
class SmsChannel |
|||
{ |
|||
/** |
|||
* The API URL for Socket. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $sms_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 $sms_url) |
|||
{ |
|||
$this->http = $http; |
|||
$this->sms_url = $sms_url; |
|||
} |
|||
|
|||
/** |
|||
* Send the given notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @param \Illuminate\Notifications\Notification $notification |
|||
* @return void |
|||
*/ |
|||
public function send($notifiable, Notification $notification) |
|||
{ |
|||
$message = $notification->toSms($notifiable); |
|||
$type = $notification->getType(); |
|||
|
|||
$message->to($notifiable->routeNotificationFor('sms', $notification)); |
|||
|
|||
if (! $message->to) { |
|||
return; |
|||
} |
|||
|
|||
try { |
|||
$this->http->post($this->sms_url . 'api/' . $type , [ |
|||
'headers' => [ |
|||
'x-sms-ir-secure-token'=>self::getToken() |
|||
], |
|||
'connect_timeout' => 30, |
|||
'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,58 @@ |
|||
<?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; |
|||
|
|||
|
|||
public $type; |
|||
|
|||
/** |
|||
* Create a new notification instance. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function __construct($message, $type = 'MessageSend') |
|||
{ |
|||
$this->message = $message; |
|||
$this->type = $type; |
|||
} |
|||
|
|||
/** |
|||
* Get the notification's delivery channels. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @return array |
|||
*/ |
|||
public function via($notifiable) |
|||
{ |
|||
return ['sms']; |
|||
} |
|||
|
|||
/** |
|||
* Get the array representation of the notification. |
|||
* |
|||
* @param mixed $notifiable |
|||
* @return array |
|||
*/ |
|||
public function toSms($notifiable) |
|||
{ |
|||
return [ |
|||
|
|||
]; |
|||
} |
|||
|
|||
public function getType() |
|||
{ |
|||
return $this->type; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue