Author - StudySection Post Views - 747 views
Laravel | custom

Custom Notification in Laravel

Laravel provides a custom channel feature to send notifications. To send notifications you just need a send method. The method requires two parameters: notifiable and notification. In the send method, you just need to call the notification method.

Create CheckoutChannel class in the app/Channels directory of the project
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class CheckoutChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->sendInvoice($notifiable);
// Send notification to the $notifiable instance...
}
}

Once the notification class has been created, you just need to return the channel from the via of the notification class.
<?php
namespace App\Notifications;
use App\Channels\Messages\VoiceMessage;
use App\Channels\VoiceChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class Invoice extends Notification
{
use Queueable;
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [CheckoutChannel::class];
}
/**
* Get the voice representation of the notification.
*
* @param mixed $notifiable
* @return VoiceMessage
*/
public function sendInvoice($notifiable)
{
Log::info('In sendInvoice method');
}
}

Method to test custom notification

use App\Notifications\Invoice;
public static function testCustomNotification(Request $request) {
try {
$user = User::where('id',10)->first();
$user->notify(new Invoice());
} catch ( \Exception $e ) {
Log::error ( 'Exception in testCustomNotification method --- ' . $e );
}
}

StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.

Leave a Reply

Your email address will not be published.