DEV Community

Cover image for Implementing Novu Notifications with Laravel: A Step-by-Step Guide
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on

Implementing Novu Notifications with Laravel: A Step-by-Step Guide

Hello Artisan,

Today’s blog is about how to use Novu notifications. It provides a vast range of SDKs and Server Libraries. However, we will be using Laravel SDK and installing it in our laravel application.

I hope you have read part one and have an idea of how to use Novu notification. If not, here is a link Part 1: Simplify Communication with NOVU: A Developer's Guide. Make sure you read it to have a basic idea of what exactly the Novu does.

A quick overview of what we are learning in this blog post.

  1. Create project using laravel (v10)
  2. Installation on Novu Laravel SDK
  3. Usage of Novu notification

So let’s begin with the installation process.

Step 1: Create a laravel project using the following command and run a migration
Note: Create a migration to add phone field in users table

composer create-project laravel/laravel:^10.0 novu-laravel-app
//Run the migration
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

a. Create a dummy users using seeder to do that create a UserSeeder.

php artisan make:seeder UserSeeder
Enter fullscreen mode Exit fullscreen mode

b. Open UserSeeder.php file, add the given code and run the command. It will create five (5) users in our application.

\App\Models\User::factory(5)->create();

php artisan db:seed --class=UserSeeder
Enter fullscreen mode Exit fullscreen mode

Step 2: Install novu-laravel SDK using composer.

composer require novu/novu-laravel
Enter fullscreen mode Exit fullscreen mode

You can publish the configuration file using this command:

php artisan vendor:publish --tag="novu-laravel-config"
Enter fullscreen mode Exit fullscreen mode

Step 3: Usage of Novu notification in laravel

a. Create a UserController where we take all the users and send test Email and SMS to each of them.

php artisan make:controller UserController
Enter fullscreen mode Exit fullscreen mode

b. Open UserController to add a snippet of Novu workflow which we created in our last blog post.

  • We can use Novu facade or novu() helper to trigger the event.
use Novu\Laravel\Facades\Novu;
use App\Models\User;

public function sendNotification (Request $request) {
     $users = User::select('id', 'name', 'email', 'phone')
                ->get();
     foreach($users as $user) {
         // using Novu facade
        $response = Novu::triggerEvent([
           'name' => '<WORKFLOW_TRIGGER_IDENTIFIER_FROM_DASHBOARD>',
           'payload' => ['username' => $user->name],
           'to' => [
              'subscriberId' => '<SUBSCRIBER_ID_FROM_DASHBOARD>',
              'email' => $user->name,
              'phone' => $user->phone
           ]
        ])->toArray();
      }
}
Enter fullscreen mode Exit fullscreen mode
  • Make sure you have added this route in web.php
Route::post('send-notification', [UserController::class, 'sendNotification'])->name('sendNotification');
Enter fullscreen mode Exit fullscreen mode

To get the WORKFLOW_TRIGGER_IDENTIFIER_FROM_DASHBOARD go to the dashboard, click on Get Snippet and copy the name parameter. Refer to SS for a clear view.

Image description

To get a subscriberId go to the left side click on the Subscriber menu and copy the subscription id.

Image description

Congratulations! You've now successfully integrated Novu notification infrastructure into your Laravel application. Feel free to explore its capabilities for other communication channels, and for further guidance, refer to Novu's comprehensive documentation for detailed instructions. Novu Documentation

Furthermore, you can explore additional details on my GitHub repository, where I've crafted a project named the Daily Quote Application.

Overview of Project:

  • In this project, users can subscribe to the Daily Quotes Application.
  • Upon subscription, users will receive a welcome email, while the project admin will be notified via email and SMS.
  • Subscribed users will receive a daily quote via email.
  • To facilitate this, I've developed a command which has been scheduled to run daily at a specific time. Github Project Link

Hope you find this article useful! 😊 Thanks for reading!

Happy Coding!!
Happy Reading!!
🦄 ❤️

Top comments (0)