Skip to main content

User Notifications

Peak provides a user-friendly notification interface built on top of Laravel’s default notification system. This allows you to send and display dynamic notifications to your users within the application.

When to Use Notifications

Notifications help improve user engagement by keeping users informed of important events. You can customize when and why users receive notifications based on your app's needs. Here are a few example use cases:

  • Alert users when a new reply is added to a forum post they follow.
  • Notify users when someone follows them.
  • Inform users when they receive a new message.

Ultimately, you decide how to use notifications to enhance the user experience.

Creating Notifications

Peak leverages Laravel’s built-in notification functionality, which is both flexible and simple to use.

For a deeper understanding, check out the Laravel Notifications Documentation.

To generate a new notification class, use the artisan command:

php artisan make:notification SubscribedNotification

This will create a file located at:

app/Notifications/SubscribedNotification.php

For example, a default TestNotification may already be present in your application. Here’s how it’s structured:

Update the Delivery Channels

By default, Laravel sends notifications via mail. To use database notifications, update the via() method:

Before:

public function via($notifiable)
{
return ['mail'];
}

After:

public function via($notifiable)
{
return ['database'];
}

Customize the Notification Payload

Modify the toArray() method to define how your notification data will be stored and displayed:

public function toArray($notifiable)
{
return [
'icon' => '/storage/users/default.png',
'body' => 'This is an example notification. Clicking it will take the user to a specific page.',
'link' => '/dashboard',
'user' => [
'name' => 'John Doe'
]
];
}

You can change any values in this array, just make sure they align with what your notification view expects—typically located in resources/js/Themes/Breeze/Pages/Dashboard/Account/Notifications.vue.

Triggering Notifications

To send a notification manually, use Laravel Tinker:

php artisan tinker

Then run:

App\Models\User::find(1)->notify(new App\Notifications\TestNotification);

Replace 1 with the ID of the user you want to notify. You can run this command multiple times to test different notifications.

Viewing Notifications

Logged-in users can access their notifications by visiting the /notifications route. This page displays all notifications sent to the user.

Notification Count

To get the number of unread notifications for the current user, use:

auth()->user()->unreadNotifications->count()

This is useful for displaying a badge or indicator in your app's navigation or header.

By integrating Laravel’s notification system with Peak’s UI, you can provide actionable feedback and keep your users engaged with important updates.