This is an easy to use package working with Laravel for sending push notification with FCM (Firebase Cloud Messaging).
Step 1 : Install package & configure
First get latest version of package on current project with the use of composer, use following command for installation.
composer require brozot/laravel-fcm
After successfully installed we need to register provider in laravel config.
"providers" => [
...
LaravelFCM\FCMServiceProvider::class,
];
& add facade alias in the same config file :
"aliases" => [
...
"FCM" => LaravelFCM\Facades\FCM::class,
];
Publish the package config file, use following command :
php artisan vendor:publish --provider="LaravelFCM\FCMServiceProvider"
Step 2 : Configuration of package
For sending push notification need server & sender key from firebase.
Add those keys in .env file
FCM_SERVER_KEY=qwerty
FCM_SENDER_ID=123456
Step 3 : Push notification
Use following code for push notification.
use FCM;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
/*...USER TOKEN...*/
$tokenArr = User::pluck("fcm_id")->toArray();
/*...OPTIONS...*/
$optionsBuilder = new OptionsBuilder();
$optionsBuilder->setTimeToLive(60 * 20);
/*...PAYLOAD BUILDER...*/
$payloadNotificationBuilder = new PayloadNotificationBuilder("Title");
$payloadNotificationBuilder->setBody("Hello users")->setSound("default");
/*...PAYLOAD DATA...*/
$payloadDataBuilder = new PayloadDataBuilder();
$payloadDataBuilder->addData(["key" => "value"]);
/*...BUILD...*/
$optionsBuilder = $optionsBuilder->build();
$payloadNotificationBuilder = $payloadNotificationBuilder->build();
$payloadDataBuilder = $payloadDataBuilder->build();
/*...PUSH NOTIFICATION...*/
$_FCMresponse = FCM::sendTo($tokenArr, $optionsBuilder, $payloadNotificationBuilder, $payloadDataBuilder);
/*...RESPONSE...*/
/*...SUCCESS & FAILURE COUNT...*/
$_FCMresponse->numberSuccess();
$_FCMresponse->numberFailure();
$_FCMresponse->numberModification();
/*...DELETE TOKEN FROM DB...*/
$_FCMresponse->tokensToDelete();
$_FCMresponse->tokensWithError();
/*...UPDATE TOKEN IN DB...*/
$_FCMresponse->tokensToModify();
/*...RECALL PUSH NOTIFICATION FOR FOLLOWING TOKENS...*/
$_FCMresponse->tokensToRetry();
For more info, use firebase official documentation