1. Go to this page and download the library: Download zanko-khaledi/notifications 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/ */
use App\Models\User;
use ZankoKhaledi\Notifications\Contracts\NotificationInterface;
use ZankoKhaledi\Notifications\NotificationService;
use App\Notifications\Telegram;
use App\Notifications\System;
$user = auth()->user();
$notifiables = User::query()->where("id", ">", 2)->get();
$notificationService = app(NotificationService::class);
// notifications sent via queue & background processing with pool
$notificationService->pool([
Telegram::class,
System::class
])->then(fn(NotificationInterface $notification) =>
$notification->setUser($user)
->setTitle("Hi")
->setMessage("Hello World")
->setNotifiable($notifiables)
);
use ZankoKhaledi\Notifications\Notification;
use ZankoKhaledi\Notifications\Contracts\NotificationInterface;
class SystemNotification extends Notification implements NotificationInterface
{
#[\Override]
public function send() : \ZankoKhaledi\Notifications\Models\Notification
{
$model = parent::send();
// you can send you're notification via broadcast channel to websocket server then any consumers could catch the notification
broadcast(new NotificationEvent($model));
return $model;
}
}
namespace App\Services\Notifications;
use Exception;
use Illuminate\Support\Facades\Http;
use ZankoKhaledi\Notifications\Contracts\NotificationInterface;
use ZankoKhaledi\Notifications\Models\Notification as ModelsNotification;
use ZankoKhaledi\Notifications\Notification;
class Telegram extends Notification implements NotificationInterface
{
#[\Override]
public function send(): ModelsNotification
{
$model = parent::send();
$url = config('notifications.telegram_url', 'https://api.telegram.org/bot'.env('TELEGRAM_BOT_TOKEN').'/sendMessage');
$response = Http::post($url, [
'chat_id' => $this->getUser()?->id,
'text' => $this->getMessage(),
]);
if ($response->successful()) {
return $model;
}
throw new Exception(sprintf('Telegram API error: %s', $response->body()));
}
}