Laravel notification manager
In addition to Laravel’s default notifications, this package adds further attributes like seen_at
, is_prioritised
, is_muted
, preview_type
and alert_type
. These attributes can be used to classify notifications for a better user experience. Furthermore, using this package you can manage subscriptions to certain notifications based on user preference, meaning that a user can specify whether or not to receive a certain notification type, channel or even a specific notification.
Features
- Manage subscribers
- Manage notification priorities
- Manage muted notifications (mute, unmute)
- Classifies notifications according to the way they appear
- Classifies notifications according where they appear
- Mark as seen
Installation
You can install the package via composer:
composer require rubik-llc/laravel-notification-manager
You can publish and run the migrations with:
php artisan vendor:publish --tag="notification-manager-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="notification-manager-config"
This is the contents of the published config file:
return [
/*
|--------------------------------------------------------------------------
| Subscribable notifications
|--------------------------------------------------------------------------
|
| All notifications which we would like to be subscribable must be placed here.
| If artisan command is used to create subscribable notification this will be autofilled
|
| Example:
| 'subscribable_notifications' => [
| 'order.accepted' => Rubik\NotificationManager\Tests\TestSupport\Notifications\OrderApprovedSubscribableNotification,
| 'order.rejected' => Rubik\NotificationManager\Tests\TestSupport\Notifications\OrderRejectedSubscribableNotification,
| ],
*/
'subscribable_notifications' => [
],
/*
|--------------------------------------------------------------------------
| Channels
|--------------------------------------------------------------------------
|
| All available channels must be placed here
| A notification will be sent to all these channels if model is subscribed to all channel("*").
| Example
| 'channels' => "database,broadcast",
|
*/
'channels' => "",
];
Usage
- Use our “Notifiable” trait in all models you wish to send notifications(most cases Users)
- This can be done by changing the import from “use Illuminate\Notifications\Notifiable;” to “use Rubik\NotificationManager\Traits\Notifiable;”, and also if not yet use the trait “use Notifiable”;. Your model should look like
- Use HasNotificationSubscription trait in all models you wish to send notifications
- Create subscribale notification by using “-s” flag in the default artisan command to create a notification.
- Add this notification to config file
php artisan make:notification SubscribaleNotification -s
From now on everything is the same as a normal notification. Below you can see how your Model and Notification should look like:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Rubik\NotificationManager\Contracts\SubscribableNotificationContract;
use Rubik\NotificationManager\Traits\SubscribableNotification;
class TestNotification extends Notification implements SubscribableNotificationContract
{
use Queueable, SubscribableNotification;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Subscribable type based on the name in config file
*
* @return string
*/
public static function subscribableNotificationType(): string
{
return 'test';
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
If you want to convert a notification to a subscribable notification all you have to do is add SubscribaleNotification Contract and implement all methods required, and use SubscribaleNotification trait. Do not forget to add this notifion to your config. Your notification should look like:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Rubik\NotificationManager\Contracts\SubscribableNotificationContract;
use Rubik\NotificationManager\Traits\SubscribableNotification;
class TestNotification extends Notification implements SubscribableNotificationContract
{
use Queueable, SubscribableNotification;
//Your code here
/**
* Subscribable type based on the name in config file
*
* @return string
*/
public static function subscribableNotificationType(): string
{
return 'test';
}
}
All changes will affect only future notifications, and if not specified different changes will affect the desired notification of the authenticated user. Will be explained below.
Subscribers/Unsubscribe
Subscribe to a notification:
NotificationManager::subscribe(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::subscribe();
Unsubscribe to a notification:
NotificationManager::unsubscribe(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::unsubscribe();
Subscribe a user to a notification:
NotificationManager::for(User::find(1))->subscribe(OrderApprovedSubscribableNotification::class);
Unsubscribe a user to a notification:
NotificationManager::for(User::find(1))->unsubscribe(OrderApprovedSubscribableNotification::class);
Subscribe to all notifications:
NotificationManager::subscribeAll();
Unsubscribe to all notifications:
NotificationManager::unsubscribeAll();
Subscribe a user to all notifications:
NotificationManager::for(User::find(1))->subscribeAll();
Unsubscribe a user to all notifications:
NotificationManager::for(User::find(1))->unsubscribeAll();
Send notification to all subscribers:
Instead of using:
Notification::send(User::subscribers()->get(),new OrderApprovedSubscribableNotification($payload));
Use:
OrderApprovedSubscribableNotification::sendToSubscribers($payload)
Everything passed to send to subscriber will be passed to notification constructor.
Priority
Set priority to a notification:
NotificationManager::prioritize(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::prioritize();
Unset priority to a notification:
NotificationManager::trivialize(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::trivialize();
Set priority to a notification as a user:
NotificationManager::for(User::find(1))->prioritize(OrderApprovedSubscribableNotification::class);
Unset priority to a notification as a user:
NotificationManager::for(User::find(1))->trivialize(OrderApprovedSubscribableNotification::class);
Mute
Mute a notification:
NotificationManager::mute(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::mute();
Unmute a notification:
NotificationManager::unmute(OrderApprovedSubscribableNotification::class);
or:
OrderApprovedSubscribableNotification::unmute();
Mute a notification for a user:
NotificationManager::for(User::find(1))->mute(OrderApprovedSubscribableNotification::class);
Unmute a notification for a user:
NotificationManager::for(User::find(1))->unmute(OrderApprovedSubscribableNotification::class);
Alert type
Set/update alert type:
NotificationManager::alertType(OrderApprovedSubscribableNotification::class, NotificationAlertType::BANNER);
or:
OrderApprovedSubscribableNotification::alertType(NotificationAlertType::BANNER);
Available alert types:
NotificationAlertType::NOTIFICATION_CENTER;
NotificationAlertType::BANNER;
NotificationAlertType::LOCK_SCREEN;
those are only values and in no way they represent a logic. You can use those values to classify notifications.
Preview type
Set/update preview type:
NotificationManager::previewType(OrderApprovedSubscribableNotification::class, NotificationPreviewType::ALWAYS);
or:
OrderApprovedSubscribableNotification::previewType(NotificationPreviewType::ALWAYS);
Available alert types:
NotificationPreviewType::ALWAYS;
NotificationPreviewType::WHEN_UNLOCKED;
NotificationPreviewType::NEVER;
Seen
Mark as seen
User::find(1)->notifications()->markAsSeen();
or:
DatabaseNotification::all()->markAsSeen();
or:
$user=User::find(1);
$user->unseenNotifications()->update(['seen_at' => now()]);
or:
$user = App\Models\User::find(1);
foreach ($user->unseenNotifications as $notification) {
$notification->markAsSeen();
}
Mark as unseen
User::find(1)->notifications()->markAsUnseen();
or:
DatabaseNotification::all()->markAsUnseen();
or:
$user->seenNotifications()->update(['seen_at' => null]);
or:
$user = App\Models\User::find(1);
foreach ($user->seenNotifications as $notification) {
$notification->markAsUnseen();
}
Get all seen notifications:
User::find(1)->seenNotifications();
Get all unseen notifications:
User::find(1)->unseenNotifications();
Get all prioritized notifications:
User::find(1)->prioritizedNotifications();
Get all trivialized notifications:
User::find(1)->trivializedNotifications();
Get all muted notifications:
User::find(1)->mutedNotifications();
Get all unmuted notifications:
User::find(1)->unmutedNotifications();
Get all notifications with alert type set to 'notification-center':
User::find(1)->alertNotificationCenterNotifications();
Get all notifications with alert type set to 'banner':
User::find(1)->alertBannerNotifications();
Get all notifications with alert type set to 'lock-screen':
User::find(1)->alertLockScreenNotifications();
Get all notifications with preview type set to 'always':
User::find(1)->previewAlwaysNotifications();
Get all notifications with preview type set to 'when-unlocked':
User::find(1)->previewWhenUnlockedNotifications();
Get all notifications with preview type set to 'never':
User::find(1)->previewNeverNotifications();
Check if a notification is seen:
User::find(1)->notifications()->first()->seen();
Check if a notification is unseen:
User::find(1)->notifications()->first()->unseen();
Check if a notification is prioritised:
User::find(1)->notifications()->first()->seen();
Check if a notification is trivialised:
User::find(1)->notifications()->first()->triavilized();
Check if a notification is muted:
User::find(1)->notifications()->first()->muted();
Check if a notification is unmuted:
User::find(1)->notifications()->first()->unmuted();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.