PHP code example of feugene / firebase-notifications-laravel

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

    

feugene / firebase-notifications-laravel example snippets




return [

    // ...

    'providers' => [
        // ...
        Feugene\FirebaseNotificationsChannel\FcmServiceProvider::class,
    ],

];



return [

    // ...

    /*
    |--------------------------------------------------------------------------
    | Firebase Settings section
    |--------------------------------------------------------------------------
    |
    | Here you may specify some configs for FCM.
    |
    */

    'fcm' => [

        /*
         |----------------------------------------------------------------------
         | Firebase service driver
         |----------------------------------------------------------------------
         |
         | Value `file` or `config`:
         |   - Select `file` option to make service read json file
         |   - Select `config` option to set up all section in config file
         |
         */

        'driver' => env('FCM_DRIVER', 'config'),

        /*
         |---------------------------------------------------------------------
         | FCM Drivers
         |---------------------------------------------------------------------
         |
         | Here are each of the firebase.
         |
         */

        'drivers' => [

            'file' => [
                'path' => env('FCM_FILE_PATH', base_path('storage/fcm.json')),
            ],

            'config' => [

                 /*
                 |------------------------------------------------------------
                 | Credentials
                 |------------------------------------------------------------
                 |
                 | Content of `firebase.json` file in config. Using if
                 | `fcm.driver` is `config`. All fields 



use Illuminate\Notifications\Notification;
use Feugene\FirebaseNotificationsChannel\FcmChannel;
use Feugene\FirebaseNotificationsChannel\FcmMessage;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [FcmChannel::class];
    }

    /**
     * @return FcmMessage
     */
    public function toFcm(): FcmMessage
    {
        return (new FcmMessage)
            ->setTitle('Approved!')
            ->setBody('Your account was approved!');
    }
}



use Illuminate\Notifications\Notifiable;
use Feugene\FirebaseNotificationsChannel\Receivers\FcmDeviceReceiver;
use Feugene\FirebaseNotificationsChannel\Receivers\FcmNotificationReceiverInterface;

class SomeNotifible
{
    use Notifiable;

    /**
    * Reveiver of firebase notification.
    *
    * @return FcmNotificationReceiverInterface
    */
    public function routeNotificationForFcm(): FcmNotificationReceiverInterface
    {
        return new FcmDeviceReceiver($this->firebase_token);
    }
}



class PushTopicSendCommand extends Command
{

    public function handle(): void
    {
        $topicName = 'topic-name';

        $resultList = app('firebase')->subscribe($topicName, [
            "eKYl5MT4Ra8:APA91bHZXfYo....tGMs4UxJS9LI_V",
            "eKY...UxJS9LI_F",
        ]);

        dd($resultList, app('firebase')->getLastResponse());


    }
}



namespace App\Notifications;

use Feugene\FirebaseNotificationsChannel\FcmChannel;
use Feugene\FirebaseNotificationsChannel\FcmMessage;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Notifications\Notification;

/**
 * Class PushToDeviceMsg
 * @package App\Notifications
 */
class PushToDeviceMsg extends Notification implements Arrayable
{
    /** @var string */
    protected $title;

    /** @var string */
    protected $body;

    /** @var array */
    protected $data;

    /** @var bool */
    protected $mutableContent;

    public function __construct(string $title, string $body, array $data = [], $mutableContent = true)
    {
        $this->title = $title;
        $this->body = $body;
        $this->data = $data;
        $this->mutableContent = $mutableContent;
    }

    /**
     * @return array
     */
    public function via(): array
    {
        return [FcmChannel::class];
    }

    /**
     * @return FcmMessage
     */
    public function toFcm(): FcmMessage
    {
        $msg = (new FcmMessage)
            ->setTitle($this->title)
            ->setBody($this->body)
            ->setData($this->data);

        if ($this->mutableContent) {
            $msg
                ->getApns()
                ->enableMutableContent();
            $msg
                ->getApns()
                ->setCategory('ActionRichPush');

            $msg
                ->getApns()
                ->setTitle($this->title);
            $msg
                ->getApns()
                ->setBody($this->body);

            $msg
                ->getAndroid()
                ->setHideNotification(true);
        }

        return $msg;

    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'title' => $this->title,
            'body' => $this->body,
            'data' => $this->data,
        ];
    }
}