Skip to content

Instantly share code, notes, and snippets.

@MinaWilliam
Last active January 4, 2023 09:55
Show Gist options
  • Save MinaWilliam/94dea042f1116aa25acb0fa3b9238cfd to your computer and use it in GitHub Desktop.
Save MinaWilliam/94dea042f1116aa25acb0fa3b9238cfd to your computer and use it in GitHub Desktop.
Laravel unifonic notification channel (custom notification channel example: https://laravel.com/docs/8.x/notifications#custom-channels)
<?php
return [
'unifonic' => [
'app_id' => env('UNIFONIC_APP_ID'),
'sender_id' => env('UNIFONIC_SENDER_ID')
],
];
<?php
declare(strict_types=1);
namespace App\Notifications\Channels;
use App\Notifications\Messages\UnifonicMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Http;
class UnifonicChannel
{
/**
* The sender Id notifications should be sent from.
*
* @var string
*/
protected $senderId;
public function __construct()
{
$this->senderId = config('services.unifonic.sender_id');
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
if (! $to = $notifiable->routeNotificationFor('unifonic')) {
return;
}
$message = $notification->toUnifonic($notifiable);
if (is_string($message)) {
$message = new UnifonicMessage($message);
}
$senderId = $message->senderId ?? $this->senderId;
$this->sendMessage(str_replace('-', '', $to), trim($message->content), $senderId);
}
protected function sendMessage(string $to, string $body, string $senderId = null)
{
// todo: handle unifonic failure
return Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])->post('https://api.unifonic.com/rest/Messages/Send', [
'AppSid' => config('services.unifonic.app_id'),
'Body' => $body,
'Recipient' => $to,
'SenderID' => $senderId
]);
}
}
<?php
declare(strict_types=1);
namespace App\Notifications\Messages;
class UnifonicMessage
{
/**
* The message content.
*
* @var string
*/
public string $content;
/**
* The Sender ID the message should be sent from.
*
* @var string
*/
public string $senderId;
/**
* The message type.
*
* @var string
*/
public string $type = 'text';
/**
* Create a new message instance.
*
* @param string $content
*
* @return void
*/
public function __construct($content = '')
{
$this->content = $content;
}
/**
* Set the message content.
*
* @param string $content
*
* @return $this
*/
public function content(string $content): UnifonicMessage
{
$this->content = $content;
return $this;
}
/**
* Set Sender ID the message should be sent from.
*
* @param $senderId
*
* @return $this
*/
public function senderId($senderId): UnifonicMessage
{
$this->senderId = $senderId;
return $this;
}
/**
* Set the message type.
*
* @return $this
*/
public function unicode(): UnifonicMessage
{
$this->type = 'unicode';
return $this;
}
}
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForUnifonic($notification)
{
return $this->phone;
}
}
<?php
namespace App\Http\Controllers\Api\V1\Auth;
use App\Http\Controllers\Controller;
use App\Notifications\VerifyPhoneNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
class VerifyPhoneController extends Controller
{
public function send($channel, Request $request)
{
$code = random_int(100000, 999999);
// phone verification implemantion ...
Notification::route('unifonic', $request->phone)
->notify(new VerifyPhoneNotification($code));
return $this->json(['message' => trans('phone.sent')]);
}
}
<?php
namespace App\Notifications;
use App\Notifications\Channels\UnifonicChannel;
use App\Notifications\Messages\UnifonicMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class VerifyPhoneNotification extends Notification
{
use Queueable;
/**
* The phone verification code.
*
* @var string
*/
public $code;
public function __construct(string $code)
{
$this->code = $code;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [UnifonicChannel::class];
}
public function toUnifonic($notifiable)
{
return (new UnifonicMessage())
->content(trans('phone.message') . $this->code);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment