PHP code example of meeeet-dev / larafirebase

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

    

meeeet-dev / larafirebase example snippets


use MeeeetDev\Larafirebase\Facades\Larafirebase;

class MyController
{
    private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}'];

    public function sendNotification()
    {
        return Larafirebase::withTitle('Hello World')
            ->withBody('I have something new to share with you!')
            ->withImage('https://firebase.google.com/images/social.png')
            ->withAdditionalData([
                'name' => 'wrench',
                'mass' => '1.3kg',
                'count' => '3'
            ])
            ->sendNotification($this->deviceTokens);
        
        // Or
        return Larafirebase::fromRaw([
            // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
            "name" => "string",
            "data" => [
                "string" => "string",
            ],
            "notification" => [
                "object" => "(Notification)"
            ],
            "android" => [
                "object" => "(AndroidConfig)"
            ],
            "webpush" => [
                "object" => "(WebpushConfig)",
            ],
            "apns" => [
                "object" => "(ApnsConfig)"
            ],
            "fcm_options" => [
                "object" => "(FcmOptions)"
            ],
            "token" => "string",
            "topic" => "string",
            "condition" => "string"
        ])->sendNotification($this->deviceTokens);
    }
}

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Traits\HasFirebaseNotification;

class OrderConfirmed extends Notification
{
    use HasFirebaseNotification;

    public $title = 'Order Confirmed';
    public $body;
    public $image;
    public $data;

    public function __construct($orderNumber)
    {
        $this->body = "Your order #{$orderNumber} has been confirmed!";
        $this->data = ['order_number' => $orderNumber];
    }

    public function via($notifiable)
    {
        return ['firebase'];
    }
}

$user->notify(new OrderConfirmed('12345'));

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Traits\HasFirebaseNotification;

class WelcomeNotification extends Notification
{
    use HasFirebaseNotification;

    private $user;
    private $referralCode;

    public function __construct($user, $referralCode = null)
    {
        $this->user = $user;
        $this->referralCode = $referralCode;
    }

    public function via($notifiable)
    {
        return ['firebase'];
    }

    protected function getFirebaseTitle($notifiable): string
    {
        return "Welcome, {$this->user->name}! 🎉";
    }

    protected function getFirebaseBody($notifiable): string
    {
        return $this->referralCode 
            ? "Thanks for joining via referral code: {$this->referralCode}"
            : "We're excited to have you on board!";
    }

    protected function getFirebaseImage($notifiable): ?string
    {
        return 'https://example.com/welcome-banner.png';
    }

    protected function getFirebaseData($notifiable): ?array
    {
        return [
            'user_id' => $this->user->id,
            'referral_code' => $this->referralCode,
            'action' => 'open_profile',
            'timestamp' => now()->toIso8601String()
        ];
    }

    protected function getFirebaseTokens($notifiable)
    {
        // Get all active device tokens
        return $notifiable->deviceTokens()
            ->where('is_active', true)
            ->pluck('token')
            ->toArray();
    }
}

$user->notify(new WelcomeNotification($user, 'REF123'));

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Messages\FirebaseMessage;

class SendBirthdayReminder extends Notification
{
    /**
     * Get the notification's delivery channels.
     */
    public function via($notifiable)
    {
        return ['firebase'];
    }

    /**
     * Get the firebase representation of the notification.
     */
    public function toFirebase($notifiable)
    {
        $deviceTokens = [
            '{TOKEN_1}',
            '{TOKEN_2}'
        ];

        return (new FirebaseMessage)
            ->withTitle('Hey, ', $notifiable->first_name)
            ->withBody('Happy Birthday!')
            ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens);
    }
}

use MeeeetDev\Larafirebase\Config\AndroidConfig;
use MeeeetDev\Larafirebase\Config\AndroidNotification;

class OrderNotification extends Notification
{
    use HasFirebaseNotification;

    protected function getAndroidConfig($notifiable): ?AndroidConfig
    {
        $androidNotification = (new AndroidNotification)
            ->withTitle('Order Confirmed')
            ->withBody('Your order is on the way!')
            ->withColor('#4CAF50')              // Notification color
            ->withSound('order_sound')          // Custom sound
            ->withChannelId('orders')           // Notification channel
            ->withClickAction('OPEN_ORDER')     // Click action
            ->withNotificationPriority('PRIORITY_HIGH')
            ->withLightSettings('#4CAF50', 1000, 500); // LED color & timing

        return (new AndroidConfig)
            ->withPriority('high')              // Message priority
            ->withTtl(86400)                    // 24 hours TTL
            ->withCollapseKey('order_updates')  // Group related messages
            ->withNotification($androidNotification);
    }
}

use MeeeetDev\Larafirebase\Config\ApnsConfig;

class OrderNotification extends Notification
{
    use HasFirebaseNotification;

    protected function getApnsConfig($notifiable): ?ApnsConfig
    {
        $apsPayload = [
            'alert' => [
                'title' => 'Order Confirmed',
                'body' => 'Your order is on the way!',
            ],
            'badge' => 1,
            'sound' => 'default',
            'category' => 'ORDER_CATEGORY',
        ];

        return (new ApnsConfig)
            ->withPriority(10)                  // 5=normal, 10=high
            ->withApsPayload($apsPayload)
            ->withAnalyticsLabel('order_campaign')
            ->withImage('https://example.com/image.png');
    }
}

use MeeeetDev\Larafirebase\Config\WebpushConfig;

class OrderNotification extends Notification
{
    use HasFirebaseNotification;

    protected function getWebpushConfig($notifiable): ?WebpushConfig
    {
        $webNotification = [
            'title' => 'Order Confirmed',
            'body' => 'Your order is on the way!',
            'icon' => 'https://example.com/icon.png',
            'badge' => 'https://example.com/badge.png',
            'image' => 'https://example.com/order.png',
        ];

        return (new WebpushConfig)
            ->withNotification($webNotification)
            ->withLink('https://example.com/orders/123')
            ->withUrgency('high')
            ->withTtl(86400);
    }
}

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Traits\HasFirebaseNotification;
use MeeeetDev\Larafirebase\Config\{AndroidConfig, AndroidNotification, ApnsConfig, WebpushConfig};

class MultiPlatformNotification extends Notification
{
    use HasFirebaseNotification;

    private $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function via($notifiable)
    {
        return ['firebase'];
    }

    // Android customization
    protected function getAndroidConfig($notifiable): ?AndroidConfig
    {
        $notification = (new AndroidNotification)
            ->withColor('#4CAF50')
            ->withSound('order_sound')
            ->withChannelId('orders');

        return (new AndroidConfig)
            ->withPriority('high')
            ->withNotification($notification);
    }

    // iOS customization
    protected function getApnsConfig($notifiable): ?ApnsConfig
    {
        return (new ApnsConfig)
            ->withPriority(10)
            ->withApsPayload([
                'alert' => ['title' => 'Order Update', 'body' => 'Confirmed!'],
                'badge' => 1,
            ]);
    }

    // Web customization
    protected function getWebpushConfig($notifiable): ?WebpushConfig
    {
        return (new WebpushConfig)
            ->withLink("https://example.com/orders/{$this->order['id']}")
            ->withUrgency('high');
    }

    // Common fields
    protected function getFirebaseTitle($notifiable): string
    {
        return 'Order Confirmed';
    }

    protected function getFirebaseBody($notifiable): string
    {
        return "Order #{$this->order['id']} confirmed!";
    }

    protected function getAnalyticsLabel($notifiable): ?string
    {
        return 'order_confirmation_campaign';
    }
}

class ConditionalNotification extends Notification
{
    use HasFirebaseNotification;

    protected function getCondition($notifiable): ?string
    {
        // Send to users subscribed to both topics
        return "'news' in topics && 'breaking' in topics";
    }

    // ... other methods
}

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Traits\HasFirebaseNotification;

class YourNotification extends Notification
{
    use HasFirebaseNotification;

    public $title = 'Hello';
    public $body = 'Your message here';

    public function via($notifiable)
    {
        return ['firebase'];
    }
}

use Illuminate\Notifications\Notification;
use MeeeetDev\Larafirebase\Messages\FirebaseMessage;

class YourNotification extends Notification
{
    public function via($notifiable)
    {
        return ['firebase'];
    }

    public function toFirebase($notifiable)
    {
        $tokens = $notifiable->fcm_token;

        return (new FirebaseMessage)
            ->withTitle('Notification Title')
            ->withBody('Notification body text')
            ->asNotification($tokens);
    }
}

Larafirebase::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');

Larafirebase::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');

return Larafirebase::fromRaw([
    'token' => 'token1',
    'data' => [
        'key_1' => 'Value 1',
        'key_2' => 'Value 2'
    ],
    'android' => [
        'ttl' => '1000s',
        'priority' => 'normal',
        'notification' => [
            'key_1' => 'Value 1',
            'key_2' => 'Value 2'
        ],
    ],
])->send();
bash
php artisan vendor:publish --provider="MeeeetDev\Larafirebase\Providers\LarafirebaseServiceProvider"