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


return [

    /*
    |--------------------------------------------------------------------------
    | Firebase Project ID
    |--------------------------------------------------------------------------
    |
    | The ID of your Firebase project. This is used to construct the API endpoint
    | for sending notifications.
    |
    */

    'project_id' => env('FIREBASE_PROJECT_ID'),

    /*
    |--------------------------------------------------------------------------
    | Service Account JSON File
    |--------------------------------------------------------------------------
    |
    | The path to your Firebase service account JSON file. This file is used to
    | generate an OAuth 2.0 access token for authenticating with Firebase.
    |
    */

    'service_account_file' => env('FIREBASE_SERVICE_ACCOUNT_FILE'),

];



namespace App\Notifications;

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

class TestFirebaseNotification extends Notification
{
    use Queueable;

    public $title;
    public $body;
    public $additionalData;
    public $deviceTokens;

    /**
     * Create a new notification instance.
     *
     * @param string $title
     * @param string $body
     * @param array $additionalData
     * @param array $deviceTokens
     */
    public function __construct($title, $body, $additionalData = [], $deviceTokens = [])
    {
        $this->title = $title;
        $this->body = $body;
        $this->additionalData = $additionalData;
        $this->deviceTokens = $deviceTokens;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['larabase'];
    }

    /**
     * Get the Larabase representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Akhelij\LarabaseNotification\LarabaseMessage
     */
    public function toLarabase($notifiable)
    {
        // Ensure all additional data values are strings
        $this->additionalData = array_map('strval', $this->additionalData);

        return (new LarabaseMessage())
            ->withTitle($this->title)
            ->withBody($this->body)
            ->withAdditionalData($this->additionalData)
            ->asNotification($this->deviceTokens);
    }
}

use App\Models\User;
use App\Notifications\TestFirebaseNotification;

$user = User::find(1); // Replace with the appropriate user ID

$title = 'Test Notification';
$body = 'This is a test message';
$additionalData = [
    'key1' => 'value1',
    'key2' => 'value2',
];

$deviceTokens = [$request->input('fcm_token')]; // Replace with actual tokens

$notification = new TestFirebaseNotification($title, $body, $additionalData, $deviceTokens);

$user->notify($notification);
bash
php artisan vendor:publish --provider="Akhelij\LarabaseNotification\LarabaseNotificationServiceProvider" --tag="config"
bash
php artisan make:notification TestFirebaseNotification