dev1/notify-laravel

Laravel adapter for DEV1 Notify Core

1.1.0 2025-09-23 22:05 UTC

This package is auto-updated.

Last update: 2025-09-23 22:05:54 UTC


README

Tests Coverage Latest Stable Version Total Downloads

Adapter package to integrate DEV1 Notify Core into Laravel 8+.

Provides:

  • Service Provider for Notify Core
  • Config publishing (config/notify.php)
  • Custom Laravel Notification Channel (dev1-notify)
  • Logger bridge to Laravel.

Installation

composer require dev1/notify-laravel
php artisan vendor:publish --tag=notify-config

Configuration

.env example:

NOTIFY_DEFAULT=fcm
NOTIFY_FCM_PROJECT_ID=your-firebase-project-id
NOTIFY_FCM_SA_PATH=app/firebase/service-account.json
NOTIFY_FCM_TIMEOUT=10 (optional, default 10)
NOTIFY_LOG=true (optional, default true)

For FCM v1, you need to create a Firebase project and generate a service account key JSON file at Firebase Console. After you download the JSON file, place it in a secure location within your Laravel project (e.g., storage/app/firebase/service-account.json).

Config file (config/notify.php) example:

return [
    'default' => env('NOTIFY_DEFAULT', 'fcm'),

    'clients' => [
        'fcm' => [
            'driver' => 'fcm_v1',
            'project_id' => env('NOTIFY_FCM_PROJECT_ID'),
            'service_account_json' => storage_path(env('NOTIFY_FCM_SA_PATH')),
            'scopes' => [
                'https://www.googleapis.com/auth/firebase.messaging',
            ],
            'timeout' => env('NOTIFY_FCM_TIMEOUT', 10),
            'platform_defaults' => [
                'android' => [
                    // 'priority' => 'HIGH',
                    // 'ttl' => 3600, // segundos (el transport lo convertirá a "3600s")
                    // 'notification' => ['icon' => 'ic_stat_notify'],
                ],
                'apns' => [
                    // 'headers' => ['apns-priority' => '10', 'apns-push-type' => 'alert'],
                    // 'aps'     => ['sound' => 'default', 'mutable-content' => 1],
                    // 'custom'  => [],
                ],
            ],
        ],
    ],

    'logging' => [
        'enabled' => env('NOTIFY_LOG', true),
        'channel' => env('NOTIFY_LOG_CHANNEL', null),
    ],

Usage

We have two ways to use Notify in Laravel, with a Facade or via the Notification Channel. Use the one that best fits your needs.

Via Facade:

This one is the simplest way to use Notify in Laravel, just call the Notify facade and send your notification. Recommended for a single notification.

use Notify;

use Dev1\NotifyCore\Platform\AndroidOptions;
use Dev1\NotifyCore\Platform\ApnsOptions;

$android = AndroidOptions::make()
    ->withChannelId('your_channel_id')
    ->withPriority('HIGH')
    ->withTtl(900);

$apns = ApnsOptions::make()
    ->withHeaders(['apns-priority' => '10', 'apns-push-type' => 'alert'])
    ->withAps(['sound' => 'default']);

$result = Notify::send(
    ['token' => 'AAA', 'topic' => null, 'condition' => null],
    [
        'title' => 'Hola',
        'body' => 'Mensaje de prueba',
        'data' => ['foo' => 'bar'],
        'android' => $android,
        'apns' => $apns,
    ],
    'fcm'
);

// $result is an instance of Dev1\NotifyCore\DTO\PushResult, use it for response handling.

Via Notification Channel:

This one is intended to being used with Laravel Notifications, so you can use all the features of Laravel Notifications like queues, markdown, etc.

use Illuminate\Notifications\Notification;

class OrderPaid extends Notification
{
    public function via($notifiable) { return ['dev1-notify']; }

    public function toDev1Notify($notifiable): array
    {
        $token = $notifiable->fcm_token; // Assuming your User model has a fcm_token attribute
        
        // Platform specific options (optional)
        $android = AndroidOptions::make()
            ->withChannelId('your_channel_id')
            ->withPriority('HIGH')
            ->withNotification(['image' => 'https://cdn.example.com/paid.png']);

        $apns = ApnsOptions::make()
            ->withHeaders(['apns-priority' => '10', 'apns-push-type' => 'alert'])
            ->withAps(['sound' => 'default']);

        return [
            'client' => 'fcm',
            'target' => [
                'token' => , $token,
                'topic' => null,
                'condition' => null,
            ],
            'payload' => [
                'title' => 'Payment Received',
                'body' => "Your order has been paid. Enjoy!",
                'data' => ['order_id' => 123], // Optional custom data
                'android' => $android,
                'apns' => $apns,
            ],
        ];
    }
}

Events

Every push sent through dev1-notify channel dispatches:

Dev1\NotifyLaravel\Events\NotifySent event, which contains the following properties:

  • $notifiable: The notifiable entity (e.g., User model).
  • $notification: The notification instance.
  • $pushResult: An instance of Dev1\NotifyCore\DTO\PushResult, containing details about the push result.

Testing

You can run the tests with:

./vendor/bin/phpunit

CI & CD

CI is provided with GitHub Actions, it runs on every push and pull request to the master branch. It runs the tests and generates code coverage reports.

  • PHPUnit with Orchestra Testbench
  • Enforces ≥ 80% coverage
  • Coverage badge updated via Gist

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repo and create a feature branch.
  2. Run tests with ./vendor/bin/phpunit.
  3. Ensure coverage ≥ 80%.
  4. Submit a PR.

Issues and suggestions are welcome on GitHub.

Made with ❤️ in Mexico 🇲🇽 by DEV1 Softworks Labs