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.
- 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
- 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.
- 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.
-
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.
-
Troubleshooting: If the notification is not sending to the device, there are a few things you can check:
- Make sure the FCM API key and auth secret are correct.
- Make sure the device token is correct and belongs to the device you want to send the notification to.
- Make sure the device is registered with Firebase Cloud Messaging and has the correct app installed.
- Make sure the device's notification settings allow it to receive notifications from the app.
- Make sure the Laravel application is running and the notification route is accessible.
- Make sure the notification is being queued and processed correctly by Laravel. You can check the Laravel queue logs for any errors.
- Make sure the device's FCM registration token is being stored correctly in the database and is being retrieved correctly when sending the notification.
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.