1. Go to this page and download the library: Download paxha/laravel-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/ */
paxha / laravel-fcm example snippets
'fcm' => [ // this fcm key is the channel name you can create multiple channels over here...
'project' => env('GOOGLE_PROJECT'),
'service_account' => env('GOOGLE_SERVICE_ACCOUNT'),
],
class YourModel extends Model
{
use \LaravelFCM\Traits\HasPushToken;
// if your model has a push token column name other than `push_token` then you can define it like this
public function routeNotificationForFCM()
{
return $this->your_custom_column; // column name where you stored the push token
}
}
namespace App\Notifications;
use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Notification;
class NewMessage extends Notification implements ShouldQueue
{
use Queueable;
// take params according to your t.
public function toFCM()
{
return [
'title' => $this->title,
'body' => $this->body,
];
}
// optional: you can use according to your ata() {
return $this->data;
}
}
public function handle(object $event): void
{
// you can the notifiable instance
$notifiable = $event->notifiable;
// you can get the notification instance
$notification = $event->notification;
// you can get the message instance. This is the message that was sent to FCM
$message = $event->message;
// you can get the response of the notification
$response = $event->response;
// you can also do some other stuff after sending the notification
}