In the previous article, I introduced a Backpack's new Menu Dropdown Column component which I use for my e-commerce admin panel. Today,...
In the previous article, I introduced a Backpack's new Menu Dropdown Column component which I use for my e-commerce admin panel. Today, I will talk about a Laravel feature I use to receive Order Notifications. Whenever an order comes, my team is notified to ship it ASAP. You can assume the importance of receiving quick notifications for such events.
I also use it in:
Laravel officially supports it. Thus, many laravel packages supports it too.
In this tutorial, I assume you have a basic knowledge of Laravel Notifications and you know about Slack. Slack is a messaging app designed for business use. It can be used on desktop and mobile devices, making communications easy.
First, we need a Slack account to create a new #Channel & our Slack App. The following steps will help you to create one for your Laravel app.
Steps | Screenshot |
---|---|
1. Sign in on Slack & Start with free plan. | |
2. Go to https://api.slack.com/apps. Click on Create an app & select From scratch as we are building a simple app. | |
3. Next, you will be asked to create or select a workspace. Just name it. | |
4. After workspace, You can Create an App for that. | |
5. Name your Slack App & choose the workspace. | |
6. Next, click on Incoming Webhooks. | |
7. Activate! Incoming Webhooks & Click "Add New Webhook to Workspace". | |
8. Choose a channel where this webhook will send messages. | |
9. Final Step! Navigate to OAuth & Permissions. Add scopes such as chat:write , incoming-webhook ,chat:write.public and copy the OAuth Token. |
Done! You need to paste the Token & Webhook URL into Your Laravel App to send notifications.
Install the official Slack Notification Channel package:
composer require laravel/slack-notification-channel
Use them within the slack configuration array in config/services.php
. Your Laravel App is now ready to send notifications.
'slack' => [
'notifications' => [
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],
In general, multiple channels are created depending on the type of notification. I created two more channels & two webhooks for each channel. For example: #orders
, #errors-logs
, #backup-logs
.
Each package that supports Slack notifications comes with a config file where we only need to configure the webhook URL.
config/backup.php
& enable the channel for package's notification class:'notifications' => [
- \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['mail'],
+ \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['slack'],
...
],
'slack' => [
+ 'webhook_url' => env('SLACK_BOT_BACKUP_WEBHOOK_URL'),
...
],
config/logging.php
to enable it:'slack' => [
+ 'url' => env('SLACK_BOT_ERROR_LOG_WEBHOOK_URL'),
...
],
Thats it! These packages will be sending the notifications on their configured events.🔔
Now let's talk about creating Notification as per our needs.
php artisan make:notification OrderNotification
toSlack()
method and format the notification:use Illuminate\Notifications\Slack\SlackMessage;
public function toSlack($notifiable)
{
return (new SlackMessage)
->to("#orders") // set the channel
->text('Check & update order status.')
->headerBlock('#'.$this->order->id.' Order Received🎉');
}
You can find a set of blocks here for formatting beautiful Slack notifications. You can also give action choices to the user with
actionsBlock()
available here.
slack
delivery channel for the notification:public function via($notifiable)
{
- return ['mail'];
+ return ['mail', 'slack'];
}
Tip: You can turn channels on/off for each Notification by conditionally altering the array.
Done! Our Notification is ready to send on Slack.
For a better understanding of
Notificaton::class
, You can check this Gist. I use it to sendSMS
to user and receiveSlack
notification when an order gets placed.
Notifiable
trait, For example we can use this in User Model:<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
+ use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
+ use Notifiable;
}
and then send (☝️ check Gist to see how i pass order information.):
$user->notify(new OrderNotification($order));
use Illuminate\Support\Facades\Notification;
Notification::send($users, new OrderNotification($order));
Notification::route('slack', env('SLACK_WEBHOOK'))->notify(new SimpleNotification());
You can find a set of blocks here for formatting beautiful Slack Notifications. You can also give action choices to the user with actionsBlock()
available here.
We often need to get notified when something happens on the App. Get timely updates from your Laravel application with the help of Slack notifications. You can set it up in just 10 minutes!
I hope this article was useful and saved you some time figuring out this yourself. Just don't get into Bombing your Slack channels😝!
Happy Coding!
Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.
What do you think about this?
Wondering what our community has been up to?