Browse Source

add basic sms notification and channel

mahdi
mahdihty 4 years ago
parent
commit
ecea4065fe
  1. 68
      app/Channels/SmsChannel.php
  2. 31
      app/Notifications/SmsNotification.php

68
app/Channels/SmsChannel.php

@ -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);
}
}
}

31
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;
}
}
Loading…
Cancel
Save