PHP code example of cofa / notification_via_firebase_and_database

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

    

cofa / notification_via_firebase_and_database example snippets


'providers' => [
    Cofa\NotificationViaFirebaseAndDatabase\FirebaseNotificationServiceProvider::class,
],

return [
    'firebase' => [
        'credentials' => storage_path('app/firebase/credentials.json'),
    ],
];

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',
        ];
    }
}

$user->notify(new OrderShipped(['order_id' => 12345]));

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();

$payload = new FirebasePayload();

$payload->setData([
    'notification' => ['title' => 'Hello', 'body' => 'World'],
    'data'         => ['deep_link' => 'app://home'],
]);

// Android
$payload->setAndroidConfiguration([
    'priority'     => 'high',
    'notification' => ['sound' => 'default', 'color' => '#FF5722'],
]);

// iOS
$payload->setIOSConfiguration([
    'headers' => ['apns-priority' => '10'],
    'payload' => [
        'aps' => ['sound' => 'default', 'badge' => 1],
    ],
]);

try {
    $user->notify(new OrderShipped($orderData));
} catch (\RuntimeException $e) {
    Log::error('Notification failed', [
        'user_id' => $user->id,
        'error'   => $e->getMessage(),
    ]);
}

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
    }
}
bash
php artisan vendor:publish --tag=firebase-notification-config
bash
php artisan firebase-notification:install
php artisan migrate