From ecea4065fe856d84e95ca8ce87ca22b30e91768f Mon Sep 17 00:00:00 2001 From: mahdihty Date: Mon, 15 Mar 2021 19:14:11 +0330 Subject: [PATCH] add basic sms notification and channel --- app/Channels/SmsChannel.php | 68 +++++++++++++++++++++++++++ app/Notifications/SmsNotification.php | 31 +++++------- 2 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 app/Channels/SmsChannel.php diff --git a/app/Channels/SmsChannel.php b/app/Channels/SmsChannel.php new file mode 100644 index 0000000..49d72d4 --- /dev/null +++ b/app/Channels/SmsChannel.php @@ -0,0 +1,68 @@ +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); + } + + } +} diff --git a/app/Notifications/SmsNotification.php b/app/Notifications/SmsNotification.php index eb9be1a..f44abcb 100644 --- a/app/Notifications/SmsNotification.php +++ b/app/Notifications/SmsNotification.php @@ -13,14 +13,18 @@ class SmsNotification extends Notification public $message; + + public $type; + /** * Create a new notification instance. * * @return void */ - public function __construct($message) + public function __construct($message, $type = 'MessageSend') { $this->message = $message; + $this->type = $type; } /** @@ -31,21 +35,7 @@ class SmsNotification extends Notification */ 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!'); + return ['sms']; } /** @@ -54,10 +44,15 @@ class SmsNotification extends Notification * @param mixed $notifiable * @return array */ - public function toArray($notifiable) + public function toSms($notifiable) { return [ - // + ]; } + + public function getType() + { + return $this->type; + } }