PHP code example of webazin / notify-server-client

1. Go to this page and download the library: Download webazin/notify-server-client 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/ */

    

webazin / notify-server-client example snippets


use Webazin\NotifyServer\Facades\NotifyServer;

// ارسال اعلان
$result = NotifyServer::send(
    title: 'سفارش شما ثبت شد',
    message: 'سفارش شماره ۱۰۲۳ با موفقیت ثبت شد.',
    type: 'success',
    payload: ['order_id' => 1023, 'amount' => 250000],
);
// $result['notification_id']

// تولید کد اتصال برای کاربر جدید
$code = NotifyServer::generateConnectionCode();
// $code['code'], $code['expires_at']

use Webazin\NotifyServer\NotifyServerClient;

class OrderController
{
    public function store(NotifyServerClient $notify)
    {
        // ...
        $notify->send('سفارش ثبت شد', 'سفارش شما با موفقیت ثبت شد.', 'success');
    }
}



namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Webazin\NotifyServer\Notifications\NotifyServerMessage;

class OrderCreated extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(protected int $orderId, protected int $amount)
    {
    }

    public function via(object $notifiable): array
    {
        return ['notify-server'];
    }

    public function toNotifyServer(object $notifiable): NotifyServerMessage
    {
        return NotifyServerMessage::create(
            title: 'سفارش شما ثبت شد',
            message: "سفارش شماره {$this->orderId} با موفقیت ثبت شد."
        )
            ->success()
            ->payload([
                'order_id' => $this->orderId,
                'amount' => $this->amount,
            ]);
    }
}

use App\Notifications\OrderCreated;
use Illuminate\Support\Facades\Notification;

Notification::route('notify-server', null)
    ->notify(new OrderCreated($order->id, $order->amount));

// یا روی یک مدل Notifiable (User و غیره) که trait Notifiable دارد:
$user->notify(new OrderCreated($order->id, $order->amount));

use Webazin\NotifyServer\Facades\NotifyServer;

$connection = NotifyServer::generateConnectionCode();

// نمایش $connection['code'] به کاربر و درخواست ارسال آن به ربات Bale/Telegram
// کد ۵ دقیقه اعتبار دارد

use Webazin\NotifyServer\Exceptions\NotifyServerException;

try {
    NotifyServer::send('عنوان', 'متن اعلان');
} catch (NotifyServerException $e) {
    logger()->error('ارسال اعلان ناموفق بود', [
        'status' => $e->status(),
        'message' => $e->getMessage(),
        'errors' => $e->errors(),
    ]);
}