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.
- Create project using laravel (v10)
- Installation on Novu Laravel SDK
- 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
a. Create a dummy users using seeder to do that create a UserSeeder
.
php artisan make:seeder UserSeeder
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
Step 2: Install novu-laravel SDK using composer.
composer require novu/novu-laravel
You can publish the configuration file using this command:
php artisan vendor:publish --tag="novu-laravel-config"
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
b. Open UserController
to add a snippet of Novu workflow which we created in our last blog post.
- We can use
Novu
facade ornovu()
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();
}
}
- Make sure you have added this route in
web.php
Route::post('send-notification', [UserController::class, 'sendNotification'])->name('sendNotification');
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.
To get a subscriberId
go to the left side click on the Subscriber
menu and copy the subscription id.
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)