Skip to content

πŸ”’ A Laravel package that allows you to check the notification settings before send them.

License

Notifications You must be signed in to change notification settings

williamcruzme/laravel-notification-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel Notification Settings

Laravel Laravel GitHub


laravel-notification-settings is a Laravel package that allows you to check the notification settings before send them.

πŸ’Ώ Installation

composer require williamcruzme/laravel-notification-settings

🏁 Getting Started

1. Adding trait

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;
}

2. Running migrations

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

3. Creating seeder

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,
    ]);
}

4. Adding routes

Using the Notification facade to import the routes for manage the settings:

Notification::routes();

πŸš€ Usage

Using The Notifiable Trait

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));

Using The Notification Facade

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));

🌐 Routes

Get notifications

Method URI
GET /notifications

Mark as read

Method URI
PATCH /notifications/read

Delete notification

Method URI
DELETE /notifications/{notificationId}

Get notification settings

Method URI
GET /settings/notifications

Update notification setting

Method URI
PATCH /settings/notifications/{notificationTypeId}

Body Params

{
    "status": true, // false
}

🎨 Customizing

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');

Custom request validations

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 [];
    }
}

Custom response

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);
    }
}

Custom guards

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();
    }
}

🚸 Contributing

You are welcome to contribute to this project, but before you do, please make sure you read the contribution guide.

πŸ”’ License

MIT

About

πŸ”’ A Laravel package that allows you to check the notification settings before send them.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages