yusefarianpour/the-laravel-fcm-channel

This package is abandoned and no longer maintained. No replacement package was suggested.

Send notification from laravel to firebase cloud messaging.

V3.0.1 2022-12-28 14:09 UTC

This package is auto-updated.

Last update: 2022-12-30 14:28:59 UTC


README

The Laravel Firebase Cloud Messaging(FCM) Notification Channel

Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.3+ required.

Install

This package can be installed through Composer.

composer require journalctl/laravel-to-fcm

Add your Firebase API Key to config/services.php

'fcm' => [
    'key' => 'Your Firebase Cloud Messaging token',
],

Example Usage

Use Artisan to create a notification:

php artisan make:notification SomeNotification

Return [fcm] in the public function via($notifiable) method of your notification:

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

Or :

use Journalctl\Channels\FirebaseChannel;

...

public function via($notifiable)
{
    return [FirebaseChannel::class];
}

Add the method public function toFcm($notifiable) to your notification, and return an instance of FirebaseMessage:

use Journalctl\Channels\FirebaseChannel;
use Journalctl\Channels\FirebaseMessage;

...

public function toFcm($notifiable)
{
    $message = new FirebaseMessage();

    $message
        ->title('Foo')  // Required
        ->body('Bar')   // Required
        ->sound()   // Optional
        ->icon()   // Optional
        ->clickAction();    // Optional

    $message->data([
        'param1' => 'baz' // Optional
    ])->priority(FirebaseMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

    return $message;
}

When sending to specific device, make sure your notifiable entity has routeNotificationForFcm method defined:

/**
 * Route notifications for the Firebase Cloud Messaging channel.
 *
 * @return string
 */
public function routeNotificationForFcm()
{
    return $this->device_token;
}

License

The "The Laravel FCM Channel" is open-sourced software licensed under the MIT license.