PHP code example of akhelij / larabase-notification

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

    

akhelij / larabase-notification example snippets




namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Akhelij\LarabaseNotification\LarabaseMessage;

class OrderShipped extends Notification
{
    use Queueable;

    public function __construct(
        private string $orderId,
        private array $deviceTokens,
    ) {}

    public function via($notifiable): array
    {
        return ['larabase'];
    }

    public function toLarabase($notifiable): LarabaseMessage
    {
        return (new LarabaseMessage())
            ->withTitle('Order Shipped')
            ->withBody("Your order #{$this->orderId} has been shipped!")
            ->withAdditionalData([
                'order_id' => $this->orderId,
                'type' => 'order_shipped',
            ])
            ->asNotification($this->deviceTokens);
    }
}

public function toLarabase($notifiable): LarabaseMessage
{
    return (new LarabaseMessage())
        ->withTitle('New Photo')
        ->withBody('Someone shared a photo with you')
        ->withImage('https://example.com/photo.jpg')
        ->asNotification($this->tokens);
}

public function toLarabase($notifiable): LarabaseMessage
{
    return (new LarabaseMessage())
        ->withTitle('Alert')
        ->withBody('Important update')
        ->withPriority('HIGH')           // Android priority
        ->withSound('alert.caf')         // iOS sound
        ->withBadge(1)                   // iOS badge count
        ->withClickAction('OPEN_ORDER')  // Android click action
        ->asNotification($this->tokens);
}

public function toLarabase($notifiable): LarabaseMessage
{
    return (new LarabaseMessage())
        ->withTitle('Alert')
        ->withBody('Update available')
        ->withAndroid([
            'priority' => 'HIGH',
            'notification' => [
                'color' => '#ff0000',
                'click_action' => 'OPEN_ACTIVITY',
            ],
        ])
        ->withApns([
            'payload' => [
                'aps' => [
                    'sound' => 'custom.caf',
                    'badge' => 5,
                ],
            ],
        ])
        ->withWebpush([
            'notification' => [
                'icon' => '/icon-192.png',
            ],
        ])
        ->asNotification($this->tokens);
}

// app/Providers/EventServiceProvider.php
use Illuminate\Notifications\Events\NotificationFailed;
use App\Listeners\HandleFailedFcmNotification;

protected $listen = [
    NotificationFailed::class => [
        HandleFailedFcmNotification::class,
    ],
];

// app/Listeners/HandleFailedFcmNotification.php
namespace App\Listeners;

use Akhelij\LarabaseNotification\LarabaseChannel;
use Illuminate\Notifications\Events\NotificationFailed;

class HandleFailedFcmNotification
{
    public function handle(NotificationFailed $event): void
    {
        if ($event->channel !== LarabaseChannel::class) {
            return;
        }

        $token = $event->data['token'];
        $errorCode = $event->data['error_code'] ?? '';

        if ($errorCode === 'UNREGISTERED') {
            // Remove the stale token from your database
            $event->notifiable->deviceTokens()
                ->where('fcm_token', $token)
                ->delete();
        }
    }
}

// If calling the channel directly (e.g., in tests):
$report = $channel->send($notifiable, $notification);

$report->successCount();        // Number of successful deliveries
$report->failureCount();        // Number of failures
$report->hasFailures();         // bool
$report->failedTokens();        // Array of failed token strings
$report->unregisteredTokens();  // Tokens that FCM reports as unregistered

use Akhelij\LarabaseNotification\LarabaseNotification;

public function toLarabase($notifiable): LarabaseMessage
{
    $client = new LarabaseNotification(
        projectId: 'other-project-id',
        serviceAccountFile: '/path/to/other-service-account.json',
    );

    return (new LarabaseMessage())
        ->withTitle('Hello')
        ->withBody('From another project')
        ->usingClient($client)
        ->asNotification($this->tokens);
}

use Akhelij\LarabaseNotification\LarabaseNotificationFacade as Larabase;

// Send to a single token
$response = Larabase::sendNotification($token, 'Title', 'Body', ['key' => 'value']);

// Send to multiple tokens concurrently
$results = Larabase::sendToMultipleTokens($tokens, 'Title', 'Body', ['key' => 'value']);
bash
php artisan vendor:publish --provider="Akhelij\LarabaseNotification\LarabaseNotificationServiceProvider" --tag="config"