PHP code example of sevealex / laravel-fcm

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/ */

    

sevealex / laravel-fcm example snippets


'providers' => [
	Prgayman\Fcm\FcmServiceProvider::class,
]

'aliases' => [

  'FCM' => Prgayman\Fcm\Facades\Fcm::class,

]

$app->register(Prgayman\Fcm\FcmServiceProvider::class);

class_alias(\Prgayman\Fcm\Facades\Fcm::class, 'PFCM');

FIREBASE_SERVER_KEY=my_server_key


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\Models\FcmToken;

$user = User::find(1);
/**
 * Get all fcm tokens
 * @param string $locale (optional)
 *
 * @return array
 */
$fcmToken = FcmToken::getTokens("en");

/**
 * Get Platform Ios Tokens
 * @param string $locale (optional)
 *
 * @return array
 */
$fcmToken = FcmToken::getIosTokens();

/**
 * Get Platform android Tokens
 * @param string $locale (optional)
 *
 * @return array
 */
$fcmToken = FcmToken::getAndroidTokens();

/**
 * Get Platform web Tokens
 * @param string $locale (optional)
 *
 * @return array
 */
$fcmToken = FcmToken::getWebTokens();

/**
 * Create Or Update Token
 * @param string $token
 * @param Illuminate\Database\Eloquent\Model $model_type (optional)
 * @param int $model_id (optional)
 * @param string $platform (optional)
 * @param string $locale (optional)
 *
 * @return Prgayman\Fcm\Models\FcmToken
 */
$fcmToken = FcmToken::createOrUpdate($token, get_class($user), $user->id );


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();