laravel-notification-settings is a Laravel package that allows you to check the notification settings before send them.
composer require williamcruzme/laravel-notification-settings
In your user model add the Notifiable
trait. This trait supports custom guards:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Millions\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
}
php artisan migrate
Sometimes you may need to customize the migrations. Using the vendor:publish
command you can export the migrations:
php artisan vendor:publish --tag=migrations
Add all notifications that require settings. Notification that are not added will be sent without verification:
php artisan make:seeder NotificationTypesTableSeeder
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('notification_types')->insert([
'name' => 'App\\Notifications\\Welcome',
'display_text' => 'Welcome message',
'status' => true,
]);
}
Using the Notification
facade to import the routes for manage the settings:
Notification::routes();
This trait contains one method that may be used to send notifications: notify
. The notify
method check if the user wants to receive it, and expects to receive a notification instance:
use App\Notifications\InvoicePaid;
$user->notify(new InvoicePaid($invoice));
Alternatively, you may send notifications via the Notification
facade. This is useful primarily when you need to send a notification to multiple notifiable entities such as a collection of users. To send notifications using the facade, pass all of the notifiable entities and the notification instance to the send
method:
use Millions\Notifications\Facades\Notification;
Notification::send($users, new InvoicePaid($invoice));
Method | URI |
---|---|
GET | /notifications |
Method | URI |
---|---|
PATCH | /notifications/read |
Method | URI |
---|---|
DELETE | /notifications/{notificationId} |
Method | URI |
---|---|
GET | /settings/notifications |
Method | URI |
---|---|
PATCH | /settings/notifications/{notificationTypeId} |
{
"status": true, // false
}
First of all, create your own NotificationSettingController
controllers and add the ManageNotificationSettings
trait.
Second, modify the namespace of the Notification
facade routes:
Notification::routesForSettings('App\Http\Controllers');
The rules
validationErrorMessages
methods in the NotificationSettingController
allows you override the default request validations:
<?php
namespace App\Http\Controllers;
use Millions\Notifications\ManageNotificationSettings;
class NotificationSettingController extends Controller {
use ManageNotificationSettings;
/**
* Get the notification settings validation rules.
*
* @return array
*/
protected function rules()
{
return [
'status' => ['required', 'boolean'],
];
}
/**
* Get the notification settings validation error messages.
*
* @return array
*/
protected function validationErrorMessages()
{
return [];
}
}
The sendResponse
method in the NotificationSettingController
allows you override the default response:
<?php
namespace App\Http\Controllers;
use Millions\Notifications\ManageNotificationSettings;
class NotificationSettingController extends Controller {
use ManageNotificationSettings;
/**
* Get the response for a successful listing notification settings.
*
* @param array $response
* @return \Illuminate\Http\JsonResponse
*/
protected function sendResponse($response)
{
return response()->json($response);
}
}
The guard
method in the NotificationSettingController
allows you override the default guard:
<?php
namespace App\Http\Controllers;
use Millions\Notifications\ManageNotificationSettings;
class NotificationSettingController extends Controller {
use ManageNotificationSettings;
/**
* Get the guard to be used during notifications management.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return auth('admin')->guard();
}
}
You are welcome to contribute to this project, but before you do, please make sure you read the contribution guide.
MIT