PHP code example of uzinfo / ntfy-laravel

1. Go to this page and download the library: Download uzinfo/ntfy-laravel 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/ */

    

uzinfo / ntfy-laravel example snippets




namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NotificationEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public string $message,
        public int $userId,
        public ?string $title = null
    ) {}

    public function broadcastOn()
    {
        return new Channel('user.' . $this->userId);
    }

    public function broadcastWith()
    {
        return [
            'message' => $this->message,
            'title' => $this->title,
            'priority' => 4,
            'tags' => ['notification', 'alert'],
        ];
    }
}

event(new NotificationEvent('Hello World!', 123, 'Important Message'));

use UzInfo\NtfyLaravel\Ntfy;

$ntfy = new Ntfy();

// Simple notification
$ntfy->send('my-topic', 'Hello World!');

// With options
$ntfy->send('my-topic', 'Hello World!', [
    'title' => 'Important',
    'priority' => 5,
    'tags' => ['warning', 'urgent'],
    'click' => 'https://example.com',
    'icon' => 'https://example.com/icon.png'
]);

// Helper methods
$ntfy->urgent('my-topic', 'Critical alert!');
$ntfy->low('my-topic', 'Minor update');
$ntfy->markdown('my-topic', '**Bold** and *italic* text');
$ntfy->attach('my-topic', 'Check this file', 'https://example.com/file.pdf');



namespace App\Notifications;

use Illuminate\Notifications\Notification;
use UzInfo\NtfyLaravel\NtfyChannel;
use UzInfo\NtfyLaravel\NtfyMessage;

class OrderShipped extends Notification
{
    public function via($notifiable)
    {
        return [NtfyChannel::class];
    }

    public function toNtfy($notifiable)
    {
        return NtfyMessage::create('Your order has been shipped!')
            ->title('Order Update')
            ->priority(4)
            ->tags(['package', 'shipped'])
            ->click('https://example.com/track/123')
            ->icon('📦');
    }
}

class User extends Model
{
    public function routeNotificationForNtfy()
    {
        return 'user-' . $this->id;
    }
}

$user->notify(new OrderShipped());

$actions = [
    [
        'action' => 'view',
        'label' => 'Open App',
        'url' => 'https://example.com'
    ],
    [
        'action' => 'http',
        'label' => 'Update Status',
        'url' => 'https://api.example.com/update',
        'method' => 'POST'
    ]
];

$ntfy->actions('my-topic', 'Message with actions', $actions);

// config/ntfy.php
'rate_limit' => [
    'enabled' => true,
    'max_requests' => 60,
    'per_minutes' => 1,
],
bash
# Publish ntfy configuration
php artisan vendor:publish --tag=ntfy-config

# Publish broadcasting configuration (optional)
php artisan vendor:publish --tag=ntfy-broadcasting