1. Go to this page and download the library: Download sevealex/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/ */
use Illuminate\Foundation\Auth\User as Authenticatable;
use Prgayman\Fcm\Traits\HasFcm; // Add this line
class User extends Authenticatable
{
use HasFcm; // Add this line
}
$user = User::find(1);
/**
* Get all fcm tokens this user
* @param string $locale (optional)
*
* @return array
*/
$user->getFcmTokens();
/**
* Get ios fcm tokens this user
* @param string $locale (optional)
*
* @return array
*/
$user->getFcmIosTokens();
/**
* Get android fcm tokens this user
* @param string $locale (optional)
*
* @return array
*/
$user->getAndroidTokens();
/**
* Get web fcm tokens this user
* @param string $locale (optional)
*
* @return array
*/
$user->getWebTokens();
/**
* Create Or Update user fcm Token this user
* @param string $token
* @param string $platform (optional)
* @param string $locale (optional)
*
* @return Prgayman\Fcm\Models\FcmToken
*/
$fcmToken = $user->fcmCreateOrUpdate($token, "android", app()->getLocale()));
use Prgayman\Fcm\Support\Payload\Data\DataBuilder;
use Prgayman\Fcm\Support\Payload\Notification\NotificationBuilder;
use Prgayman\Fcm\Support\Payload\Options\OptionsBuilder;
use PFCM;
// Create Notification Builder
$notifyBuilder = new NotificationBuilder;
$notifyBuilder->setTitle("Notification Title");
$notifyBuilder->setBody("Notification Body");
$notifyBuilder->ios->setSubtitle("Sub title");
$notifyBuilder->setImage("image_url");
$notifyBuild = $notifyBuilder->build();
// Create Data Builder
$dataBuilder = new DataBuilder;
$dataBuilder->setData(["type" => "VIEW_PRODUCT"]);
$dataBuilder->addData(["type_id" => 1]);
$dataBuild = $dataBuilder->build();
// Create Options Builder
$optionsBuilder = new OptionsBuilder;
$optionsBuilder->setPriority("high"); // [normal|high]
$optionsBuilder->setContentAvailable(true);
$optionsBuild = $optionsBuilder->build();
// @var array|string
$tokens =[];
// Send Notification
$fcm = Fcm::sendNotification($tokens,$notifyBuild,$optionsBuild); // $optionsBuild (optional)
// Send Notification With Data
$fcm = Fcm::sendNotificationWithData($tokens,$notifyBuild,$dataBuild,$optionsBuild); // $optionsBuild (optional)
// Send Data
$fcm = Fcm::sendData($tokens,$dataBuild,$optionsBuild); // $optionsBuild (optional)
// return in number success send notification
$numberSuccess = $fcm->numberSuccess();
// return in number failure send notification
$numberFailure = $fcm->numberFailure();
// return in number modification send notification
$numberModification = $fcm->numberModification();
// return Array - you must remove all this tokens in your database
$tokensToDelete = $fcm->tokensToDelete();
// return Array (key : oldToken, value : new token - you must change the token in your database)
$tokensToModify = $fcm->tokensToModify();
// return Array - you should try to resend the message to the tokens in the array
$tokensToRetry= $fcm->tokensToRetry();
// return Array (key:token, value:error) - in production you should remove from your database the tokens
$tokensWithError = $fcm->tokensWithError();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.