PHP code example of abdogoda / laravel-notification-system

1. Go to this page and download the library: Download abdogoda/laravel-notification-system library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abdogoda / laravel-notification-system example snippets


use NotificationSystem\Facades\NotificationSystem;

NotificationSystem::make()
    ->title('Welcome to the Platform!')
    ->body('Thank you for joining our academy.')
    ->channels(['database', 'mail', 'fcm'])
    ->to($user)
    ->locale('ar')
    ->data([
        'action_url' => '/dashboard',
        'type' => 'welcome_onboarding'
    ])
    ->send();

NotificationSystem::make()
    ->title('Scheduled Maintenance Alert')
    ->body('The system will undergo scheduled maintenance tonight at 12:00 AM.')
    ->channels(['database', 'fcm'])
    ->to(['students', 'teachers', 'merchants'])
    ->send();

NotificationSystem::make()
    ->title('Urgent Alert')
    ->body('Action 

NotificationSystem::make()
    ->title('Reminder')
    ->body('Your appointment is in 1 hour.')
    ->to($user)
    ->queue(true, delaySeconds: 3600)
    ->send();

NotificationSystem::make()
    ->title('Invoice Ready')
    ->body('Your invoice is attached.')
    ->email(send: true, greeting: 'Dear Customer')
    ->attach('/path/to/invoice.pdf', name: 'Invoice.pdf', mime: 'application/pdf')
    ->to($user)
    ->send();

'default_channels' => ['database'],

'default_locale' => 'ar',
'locale_column'  => 'lang',  // Model attribute for preferred locale

'queue' => [
    'enabled'       => true,
    'connection'    => env('NOTIFICATION_QUEUE_CONNECTION', 'default'),
    'queue_name'    => env('NOTIFICATION_QUEUE_NAME', 'notifications'),
    'backoff'       => [5, 15, 60],      // Retry backoff in seconds
    'max_tries'     => 3,
    'channel_queues' => [                // Per-channel queue names
        'mail'     => 'emails',
        'fcm'      => 'push-notifications',
        'whatsapp' => 'whatsapp',
    ],
],

'guards' => [
    'admin' => [
        'model' => \App\Models\Admin::class,
        'label' => 'Admins',
    ],
    'student' => [
        'model' => \App\Models\Student::class,
        'label' => 'Students',
    ],
],

'channels' => [
    'whatsapp' => [
        'api_url' => env('BULQ_WHATSAPP_API_URL'),
        'token'   => env('BULQ_WHATSAPP_TOKEN'),
        'template_name' => env('BULQ_WHATSAPP_TEMPLATE', 'login_otp'),
    ],
    'fcm' => [
        'driver_class' => env('NOTIFICATION_FCM_DRIVER', null),
    ],
    'mail' => [
        'from_address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'from_name'    => env('MAIL_FROM_NAME', 'Laravel'),
    ],
],

'table_name'         => 'notification_logs',
'logging_enabled'    => true,
'log_retention_days' => 90,  // Set to null to keep forever

use NotificationSystem\Contracts\ChannelInterface;
use NotificationSystem\DTOs\NotificationData;
use NotificationSystem\DTOs\RecipientData;

class SlackChannel implements ChannelInterface
{
    public function send(RecipientData $recipient, NotificationData $notification): array|bool
    {
        // Your Slack sending logic here
        return ['status' => 'sent'];
    }
}

use NotificationSystem\Facades\NotificationSystem;

NotificationSystem::extend('slack', SlackChannel::class);

NotificationSystem::make()
    ->title('Hello Slack!')
    ->channels(['database', 'slack'])
    ->to($user)
    ->send();

use NotificationSystem\Contracts\FcmDriverInterface;

class MyFcmDriver implements FcmDriverInterface
{
    public function sendNotification(string $token, string $title, string $body, array $data = []): void
    {
        // Your Firebase sending logic
    }
}

$this->app->bind(FcmDriverInterface::class, MyFcmDriver::class);

use NotificationSystem\Events\NotificationSent;

class LogNotificationDelivery
{
    public function handle(NotificationSent $event): void
    {
        logger()->info('Notification delivered', [
            'notification_id' => $event->notification->id,
            'recipient_id'    => $event->recipient->id,
            'channels'        => array_keys($event->channelResults),
        ]);
    }
}

use NotificationSystem\Models\NotificationLog;

NotificationLog::delivered()->count();
NotificationLog::failed()->forChannel('mail')->get();
NotificationLog::forRecipient('App\Models\User', 1)->recent(30)->get();
NotificationLog::olderThan(90)->delete(); // Prune old logs
bash
# Publish everything
php artisan vendor:publish --provider="NotificationSystem\NotificationSystemServiceProvider"

# Or publish individually
php artisan vendor:publish --tag=notification-system-config
php artisan vendor:publish --tag=notification-system-migrations
php artisan vendor:publish --tag=notification-system-views
php artisan vendor:publish --tag=notification-system-translations
php artisan vendor:publish --tag=notification-system-assets
bash
php artisan migrate
blade
<x-notification-system-notification-card :notification="$notification" :is-unread="true" />
<x-notification-system-notification-table :notifications="$notifications" />
<x-notification-system-notification-form />
<x-notification-system-recipient-selector :guards="$guardModels" />
<x-notification-system-channel-selector :selected-channels="['database', 'mail']" />
<x-notification-system-statistics-widget :total="100" :unread="5" :delivered="90" :failed="5" />