use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForFirebase(): array
{
return $this->deviceTokens()
->where('is_active', true)
->pluck('device_token')
->toArray();
}
}
namespace App\Notifications;
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebaseNotification;
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebasePayload;
class OrderShipped extends FirebaseNotification
{
public function __construct(private array $orderData)
{
$payload = new FirebasePayload();
$payload->setData([
'notification' => [
'title' => 'Order Shipped',
'body' => "Your order #{$orderData['order_id']} has been shipped!",
],
'data' => [
'order_id' => (string) $orderData['order_id'],
'type' => 'order_shipped',
],
]);
$payload->setAndroidConfiguration(['priority' => 'high']);
$payload->setIOSConfiguration([
'headers' => ['apns-priority' => '10'],
'payload' => ['aps' => ['sound' => 'default', 'badge' => 1]],
]);
parent::__construct($payload);
}
public function via($notifiable): array
{
return ['firebase', 'database'];
}
// Optional: customize what gets stored in the database
public function toDatabase($notifiable): array
{
return [
'order_id' => $this->orderData['order_id'],
'message' => 'Your order has been shipped',
];
}
}
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebaseNotification;
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebasePayload;
class PromoAlert extends FirebaseNotification
{
public function __construct(string $message)
{
$payload = new FirebasePayload();
$payload->setData([
'notification' => ['title' => 'Special Offer', 'body' => $message],
]);
parent::__construct($payload);
}
public function via($notifiable): array
{
return ['firebase'];
}
}
$user->notify(new PromoAlert('Get 50% off today only!'));
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\DatabaseNotification;
$user->notify(new DatabaseNotification([
'type' => 'welcome',
'message' => 'Welcome to the platform!',
]));
// Get all notifications
$notifications = $user->notifications;
// Get only unread
$unread = $user->unreadNotifications;
// Mark all as read
$user->unreadNotifications->markAsRead();
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\Notification;
class SmsNotification implements Notification
{
public function __construct(private array $data) {}
public function sendNotification(array $targets): void
{
// your SMS logic here
}
}