PHP code example of amdadulhaq / fcm-laravel

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

    

amdadulhaq / fcm-laravel example snippets


use AmdadulHaq\Fcm\Contracts\HasFcmPayload;
use AmdadulHaq\Fcm\FcmMessage;
use Illuminate\Notifications\Notification;

class OrderShipped extends Notification implements HasFcmPayload
{
    public function via(object $notifiable): array
    {
        return ['database', 'fcm'];
    }

    public function toFcm(object $notifiable): FcmMessage
    {
        return FcmMessage::create('Order shipped', "Your order #{$this->order->id} is on its way!")
            ->withData(['order_id' => (string) $this->order->id]);
    }
}

public function toFcm(object $notifiable): FcmMessage
{
    return FcmMessage::create('Urgent: action needed', $this->body())
        ->withPriority('high')
        ->withChannelId('urgent_channel')
        ->withBadge($notifiable->unreadNotifications()->count());
}

public function routeNotificationForFcm(): array
{
    return $this->deviceTokens()->pluck('token')->all();
}

use AmdadulHaq\Fcm\Events\FcmTokenRejected;

class PruneRejectedFcmToken
{
    public function handle(FcmTokenRejected $event): void
    {
        DeviceToken::query()->where('token', $event->token)->delete();
    }
}

use AmdadulHaq\Fcm\FcmMessage;
use AmdadulHaq\Fcm\FcmService;

app(FcmService::class)->send(
    $deviceToken,
    FcmMessage::create('Order shipped', 'Your order is on its way!')->withData(['order_id' => '123']),
);

use AmdadulHaq\Fcm\FcmMessage;
use AmdadulHaq\Fcm\FcmService;

$fake = FcmService::fake();

// ... code under test that notifies via the "fcm" channel ...

$fake->assertSent(fn (string $token, FcmMessage $message) => $message->title === 'Order shipped');
$fake->assertSentTo($deviceToken, fn (FcmMessage $message) => $message->badge === 3);
$fake->assertSentCount(1);
$fake->assertNothingSent();
$fake->assertNotSent(fn (string $token) => $token === 'some-other-token');
bash
php artisan vendor:publish --tag=fcm-config