Laravel On-Demand Notifications

Notification is a way to notify the application users for a particular activity, event etc. It already exists on the User Model. The User Model comes with already implemented notification functionality. The user model used the Notifiable trait.

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
}

It means if you want to notify the particular user via any suitable medium such as SMS, Slack, Mail etc, then you can simply use the notify() method.

use App\Notifications\ActivityAlert;

$user->notify(new ActivityAlert($activity));

Or you can send activity alert to multiple users through Notification Facade.

Notification::send($users, new ActivityAlert($activity));

On-Demand Notifications

There are some cases when we want the On-Demand notification, Let’s say we want to notify someone who is not stored as a user. You can achieve the on-demand notification through Notification Facade. Notification facade has the route() method to achieve that.

Notification::route('mail', '[email protected]')
    ->notify(new ActivityAlert($activity));