notification not sending to devices using in laravel using fcm by laravel-notification-channels/fcm package

Updated: Jan 31, 2025

notification not sending to devices using in laravel using fcm by laravel-notification-channels/fcm package

To send notifications to devices using Firebase Cloud Messaging (FCM) in Laravel, you can use the laravel-notification-channels/fcm package. This package simplifies the process of sending FCM notifications from Laravel. Here's a step-by-step guide on how to set it up and troubleshoot common issues when notifications are not sending to devices.

  1. Install the package: First, you need to install the package using Composer. Run the following command in your terminal:
composer require laravel-notification-channels/fcm
  1. Configure the package: Next, you need to configure the package in your .env file. Add the following variables:
FCM_API_KEY=<your-fcm-api-key>
FCM_AUTH_SECRET=<your-fcm-auth-secret>

Replace <your-fcm-api-key> and <your-fcm-auth-secret> with your actual FCM API key and auth secret. You can obtain these keys from the Google Cloud Console.

  1. Create a notification: Now, you can create a notification in Laravel using the FCM channel. Here's an example of how to send a simple notification:
use App\Notifications\TestNotification;
use Illuminate\Support\Facades\Notification;

class TestController extends Controller
{
    public function sendNotification()
    {
        $user = App\User::find(1); // Replace with the actual user or device token

        Notification::route('fcm', $user->fcm_token) // Replace with the actual FCM token
                    ->notify(new TestNotification('Test Notification'));

        return 'Notification sent';
    }
}

// In app/Notifications/TestNotification.php
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\Message;
use NotificationChannels\Fcm\FcmMessage;

class TestNotification implements ShouldQueue, Message
{
    use Queueable;

    protected $message;

    /**
     * Create a new message instance.
     *
     * @param  string  $message
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

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

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'title' => 'Test Notification',
            'body' => $this->message,
        ];
    }

    /**
     * Get the FCM representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \NotificationChannels\Fcm\FcmMessage
     */
    public function toFcm($notifiable)
    {
        return FcmMessage::create()
            ->withData([
                'title' => $this->title,
                'body' => $this->message,
            ]);
    }
}

Replace App\User::find(1) with the actual user or device token you want to send the notification to.

  1. Test the notification: Now, you can test the notification by visiting the route that sends the notification in your browser or using a tool like Postman. If the notification is sent successfully, you should see the message "Notification sent" in the response.

  2. Troubleshooting: If the notification is not sending to the device, there are a few things you can check:

If you've checked all of these things and the notification is still not sending, you may need to consult the Laravel documentation or the laravel-notification-channels/fcm package documentation for more detailed troubleshooting steps.