Custom Notification in Laravel — StudySection Blog

Custom Notification in Laravel — StudySection Blog

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.

Originally published at https://studysection.com on January 7, 2022.

--

--

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Study Section

Study Section

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.