PHP code example of devkandil / notifire

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

    

devkandil / notifire example snippets


// In App\Models\User.php
protected $fillable = [
    // existing fields...
    'fcm_token',
];

use DevKandil\NotiFire\Facades\Fcm;

// Simple notification
$success = Fcm::withTitle('Hello')
    ->withBody('This is a test notification')
    ->sendNotification($fcmToken);

if ($success) {
    // Notification sent successfully
}

// Advanced notification
$success = Fcm::withTitle('Hello')
    ->withBody('This is a test notification')
    ->withImage('https://example.com/image.jpg')
    ->withIcon('notification_icon')
    ->withColor('#FF5733')
    ->withSound('default')
    ->withPriority(MessagePriority::HIGH)
    ->withAdditionalData(['key' => 'value'])
    ->sendNotification($fcmToken);

if ($success) {
    // Notification sent successfully
}

use DevKandil\NotiFire\Contracts\FcmServiceInterface;

$fcm = app(FcmServiceInterface::class);

// Simple notification
$fcm->withTitle('Hello')
    ->withBody('This is a test notification')
    ->sendNotification($fcmToken);

// Advanced notification
$fcm->withTitle('Hello')
    ->withBody('This is a test notification')
    ->withImage('https://example.com/image.jpg')
    ->withIcon('notification_icon')
    ->withColor('#FF5733')
    ->withSound('default')
    ->withPriority(MessagePriority::HIGH)
    ->withAdditionalData(['key' => 'value'])
    ->sendNotification($fcmToken);

POST /fcm/token

use DevKandil\NotiFire\Traits\HasFcm;

class User extends Model
{
    use HasFcm;
}

// Get the FCM token
$user->getFcmToken();

// Update the FCM token
$user->updateFcmToken($token);

// Send a notification
$user->sendFcmNotification(
    'Hello',
    'This is a test notification',
    [
        'image' => 'https://example.com/image.jpg',
        'sound' => 'default',
        'click_action' => 'OPEN_ACTIVITY',
        'icon' => 'notification_icon',
        'color' => '#FF5733',
        'data' => ['key' => 'value'],
        'priority' => MessagePriority::HIGH
    ]
);

use DevKandil\NotiFire\Notifications\ExampleNotification;

// Send a notification with custom title and body
$user->notify(new ExampleNotification('Welcome', 'Thank you for joining us!'));

use DevKandil\NotiFire\FcmMessage;
use DevKandil\NotiFire\Enums\MessagePriority;

public function toFcm($notifiable)
{
    return FcmMessage::create('New Message', 'You have a new message')
        ->image('https://example.com/image.jpg')
        ->sound('default')
        ->clickAction('OPEN_ACTIVITY')
        ->icon('notification_icon')
        ->color('#FF5733')
        ->priority(MessagePriority::HIGH)
        ->data([
            'message_id' => $this->message->id,
            'timestamp' => now()->toIso8601String(),
        ]);
}

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

use DevKandil\NotiFire\Traits\HasFcm;

class User extends Model
{
    use HasFcm;
}

$user->notify(new NewMessage($message));

// First, get the FCM service instance using one of these methods:
// Method 1: Using dependency injection
use DevKandil\NotiFire\Contracts\FcmServiceInterface;
$fcm = app(FcmServiceInterface::class);

// Method 2: Using the Facade
use DevKandil\NotiFire\Facades\Fcm;
$fcm = Fcm::build();

// The fromRaw method accepts a complete FCM message payload and returns the service instance
// allowing you to chain methods like send()
$response = $fcm->fromRaw([
    'message' => [
        'token' => 'device-token',
        'notification' => [
            'title' => 'Hello',
            'body' => 'This is a test notification',
        ],
        'android' => [
            'priority' => 'high',
        ],
        'data' => [
            'key' => 'value',
        ],
    ],
])->send();

if (isset($response['name'])) {
    // Notification sent successfully with message ID: $response['name']
}

$options = [
    'image' => 'https://example.com/image.jpg',
    'sound' => 'default',
    'click_action' => 'OPEN_ACTIVITY',
    'icon' => 'notification_icon',
    'color' => '#FF5733',
    'data' => ['key' => 'value'],
    'priority' => MessagePriority::HIGH
];
bash
# Publish everything
php artisan vendor:publish --provider="DevKandil\NotiFire\FcmServiceProvider"

# Or publish specific components
php artisan vendor:publish --tag=fcm-config        # Configuration file
php artisan vendor:publish --tag=fcm-migrations    # Database migrations
php artisan vendor:publish --tag=fcm-notifications # Example notification
php artisan vendor:publish --tag=fcm-contracts     # Interface contracts
php artisan vendor:publish --tag=fcm-enums         # Enums (MessagePriority)
php artisan vendor:publish --tag=fcm-traits        # Traits (HasFcm)
bash
php artisan migrate
bash
php artisan make:notification NewMessage