1. Go to this page and download the library: Download syntech/syntechfcm 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/ */
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class YourNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['fcm'];
}
/**
* Get the FCM representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toFcm($notifiable)
{
return [
'to' => $notifiable->device_token,
'notification' => [
'title' => 'Notification Title',
'body' => 'Notification Body',
'image' => '', // Optional image URL
],
];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
/**
* Route notifications for the FCM channel.
*
* @return string
*/
public function routeNotificationForFcm()
{
return $this->device_token;
}
}
use App\Notifications\YourNotification;
use App\Models\User;
// Assuming $user is an instance of the User model
$user = User::find(1); // Find the user you want to notify
$user->notify(new YourNotification($title, $body));
Schema::table('users', function (Blueprint $table) {
$table->string('device_token')->nullable();
});