PHP code example of laradevs / notifications-fcm

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

    

laradevs / notifications-fcm example snippets


// config/app.php

'Providers' => [
   // ...
   LaraDevs\Fcm\FcmServiceProvider::class,
]

// config/app.php

'aliases' => [
    // ...
    'Fcm' => LaraDevs\Fcm\FcmFacade::class,
];

return [

    /**
     * Set your FCM Server Key
     * Change to yours
     */

    'server_key' => env('FCM_SERVER_KEY', ''),
    'server_endpoint' => env('FCM_SERVER_ENDPOINT', 'https://fcm.googleapis.com/fcm/send'),
    'server_icon_app'=> env('FCM_ICON_APP')

];

FcmSendJob::dispatch('Hello World',['RECIPIENTS_IDs']);

FcmSendJob::dispatch('Hello World',['RECIPIENTS_IDs'])->onqueue('NAME_QUEUE');

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

fcm()
    ->toTopic($topic) // $topic must an string (topic name)
    ->priority('normal')
    ->timeToLive(0)
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('high')
    ->timeToLive(0)
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->notification([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
    ])
    ->send();

fcm()
    ->to($recipients) // $recipients must an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'LaradevTest',
        'body' => 'This tests laraDevs',
        'icon' => 'Your URL public to icon',
        'click_action'=>'action click'
    ])
    ->send();
bash
php artisan vendor:publish --provider="LaraDevs\Fcm\FcmServiceProvider"
javascript
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');


// Initialize Firebase
var config = {
    apiKey: "YOUR-API-KEY",
    authDomain: "YOUR-DOMAIN",
    databaseURL: "YOUR-DATABASE-URL",
    projectId: "YOUR-PROJECT-ID",
    storageBucket: "YOUR-STORAGE-BUCKET",
    messagingSenderId: "YOUR-MESSAGING-SENDER",
    appId: "YOUR-APP-ID",
    measurementId: "YOUR-MEASURE-ID"
};

firebase.initializeApp(config);
const messaging = firebase.messaging();