PHP code example of amos97 / laravel-firebase

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

    

amos97 / laravel-firebase example snippets


Plokko\LaravelFirebase\ServiceProvider::class,

Plokko\LaravelFirebase\Facades\LaravelFirebase::class,



return [
    'read_only' => env('FIREBASEDB_READONLY',false),//DEBUG

    /**
     * Firebase service account information, can be either:
     * - string : absolute path to serviceaccount json file
     * - string : content of serviceaccount (json string)
     * - array : php array conversion of the serviceaccount
     * @var array|string
     */
    'service_account' => base_path('.firebase-credentials.json'),

    /**
     * If set to true will enable Google OAuth2.0 token cache storage
     */
    'cache' => true,

    /**
     * Cache driver for OAuth token cache,
     * if null default cache driver will be used
     * @var string|null
     */
    'cache_driver' => null,

    /**
     * Specify if and what event to trigger if an invalid token is returned
     * @var string|null
     */
    'FCMInvalidTokenTriggerEvent' => null,
];


FirebaseJWT::encode($uid,['optional'=>'custom-claims-array']);


FCM::notificationTitle('My notification title')
  ->notificationBody('my notification body...');
  ->data(['notification' => 'data'])
  ->highPriority()//note: not all devices may use all the fields like priority or ttl
  ->ttl('20.5s')
  ->toDevice('my-device-fcm-token') // or toTopic('topic-name') or toCondition('condition-name') or toTarget(Target)
  ->send();//Submits the message

class TestFcmNotification extends Notification implements ShouldQueue
{
    use Queueable;
    
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [FcmNotificationChannel::class];
    }

    public function toFcm($notifiable)
    {
        return FCM::notificationTitle('Test notification')
                    ->notificationBody('notification body...')
                    ->toDevice($notifiable->deviceToken);
    }
}

$test = FirebaseDb::getReference('test'); //get the reference for item /test
$test->get('01');//Get /test/01 as an array
$test01 = $test->getReference('01');//Get a reference for /test/01
$test01->set('label','value');//Set /test/01/label = value
bash
php artisan vendor:publish --provider="Plokko\LaravelFirebase\ServiceProvider" --tag="config"