PHP code example of prgayman / larafcm

1. Go to this page and download the library: Download prgayman/larafcm 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/ */

    

prgayman / larafcm example snippets


'providers' => [
    Prgayman\LaraFcm\Providers\LaraFcmServiceProvider::class,
]

'aliases' => [

  'LaraFcm' => Prgayman\LaraFcm\Facades\LaraFcm::class,

]

$app->register(Prgayman\LaraFcm\Providers\LaraFcmServiceProvider::class);

class_alias(\Prgayman\LaraFcm\Facades\LaraFcm::class, 'LaraFcm');

LARAFCM_AUTHENTICATION_KEY=my_secret_server_key
LARAFCM_SENDER_ID=my_secret_sender_id

use Prgayman\LaraFcm\Services\LaraFcmToken;

// Store desvice token
(new LaraFcmToken)
->setTokens(['token'])
->store();

// Store desvice token to specific user
(new LaraFcmToken)
->setTokens('token')
->setModel(User::find(1))
->store();

// Can you set platform or locale to token both options is optional
(new LaraFcmToken)
->setTokens(['token'])
->setPlatform('android')
->setLocale('en')
->store();

/**
 * Get token from database you can use filter by model or locale ot platform to get tokens
 *
 * @param Illuminate\Database\Eloquent\Model|null $model
 * @param string|null $locale
 * @param string|null $platform
 *
 * @return array
 */
$tokens = LaraFcmToken::getDbTokens();

$removeTokens = [];
/**
 * Remove toknes from database
 * @param array $tokens
 *
 * @return bool
*/
$isDeleted = LaraFcmToken::removeDbTokens($removeTokens);

use Illuminate\Foundation\Auth\User as Authenticatable;
use Prgayman\LaraFcm\Traits\HasLaraFcm; // Add this line

class User extends Authenticatable
{
    use HasLaraFcm; // Add this line
}

$user = User::find(1);

/**
 * Get user tokens can you use filter by locale or platform
 *
 * @param string|null $locale
 * @param string|null $platform
 *
 * @return array
 */
$locale = null;
$platform = "ios";
$userTokens = $user->larafcmGetTokens($locale,$platform)

/**
 * store user tokens can you set platform or locale to token
 *
 * @param array|string $tokens
 * @param string|null  $locale
 * @param string|null  $platform
 *
 * @return bool
 */
$storeUserTokens = ['new_token'];
$user->larafcmStoreTokens($tokens, $locale, $platform)

use Prgayman\LaraFcm\Message\Options;
use Prgayman\LaraFcm\Message\Data;
use Prgayman\LaraFcm\Message\Notification;
use Prgayman\LaraFcm\Message\Topics;
use LaraFcm;


// Get all tokens form database return array you can set string token
$tokens = LaraFcmToken::getDbTokens();

// Send Notifications without data
$downstreamResponse = LaraFcm::to($tokens)
notification(
    (new Notification)
        ->setTitle('New Order')
        ->setBody('You have placed order')
        ->setColor('#f00')
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->send();

// Send Notifications with data
$downstreamResponse = LaraFcm::to($tokens)
notification(
    (new Notification)
        ->setTitle('New Order')
        ->setBody('You have placed order')
        ->setColor('#f00')
)
->data(
    (new Data)
    ->addData(['key'=>"value"])
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->send();

// Send data message
$downstreamResponse = LaraFcm::to($tokens)
->data(
    (new Data)
    ->addData(['key'=>"value"])
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->send();

$downstreamResponse->numberSuccess();
$downstreamResponse->numberFailure();
$downstreamResponse->numberModification();

// return Array - you must remove all this tokens in your database
$downstreamResponse->tokensToDelete();

// return Array (key : oldToken, value : new token - you must change the token in your database)
$downstreamResponse->tokensToModify();

// return Array - you should try to resend the message to the tokens in the array
$downstreamResponse->tokensToRetry();

// return Array (key:token, value:error) - in production you should remove from your database the tokens
$downstreamResponse->tokensWithError();

$topicResponse = LaraFcm::notification(
    (new Notification)
        ->setTitle('New Order')
        ->setBody('You have placed order')
        ->setColor('#f00')
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->topics(
    (new Topics)
    ->topic('larafcm')
)
->send();

$topicResponse->isSuccess();
$topicResponse->shouldRetry();
$topicResponse->error();

$topicResponse = LaraFcm::notification(
    (new Notification)
        ->setTitle('New Order')
        ->setBody('You have placed order')
        ->setColor('#f00')
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->topics(
    (new Topics)
    ->topic('larafcm')
    ->andTopic(function($condition) {
	    $condition->topic('ecommerce')->orTopic('news');
    });
)
->send();

$topicResponse->isSuccess();
$topicResponse->shouldRetry();
$topicResponse->error());


use Prgayman\LaraFcm\Message\Options;

$options = new Options;
$options->setTimeToLive(42*60)
        ->setCollapseKey('a_collapse_key');

use Prgayman\LaraFcm\Message\Notification;
$notification = new Notification();
$notification->setTitle('title')
             ->setBody('body')
             ->setSound('sound')
             ->setBadge('badge');

use Prgayman\LaraFcm\Message\Topics;

$topics = new Topics;
$topics->topic('TopicA')
       ->andTopic(function($condition) {
	       $condition->topic('TopicB')->orTopic('TopicC');
       });

$topicResponse = larafcm()
->notification(
    (new Notification)
        ->setTitle('New Order')
        ->setBody('You have placed order')
        ->setColor('#f00')
)
->options(
    (new Options)
    ->setTimeToLive(60*20)
    ->setContentAvailable(true)
)
->data(
    (new Data)
    ->addData(['key'=>"value"])
)
->topics(
    (new Topics)
    ->topic('larafcm')
)
->send();


use Illuminate\Notifications\Notification;
use  Prgayman\LaraFcm\Services\LaraFcm;

use Prgayman\LaraFcm\Message\Data;
use Prgayman\LaraFcm\Message\Notification as LaraFcmNotification;
use Prgayman\LaraFcm\Message\Options;

class SendPlacedOrder extends Notification
{
    public function via($notifiable)
    {
        return ['larafcm'];
    }

    public function toLaraFcm($notifiable)
    {
        $tokens = ['TOKEN_1','TOKEN_2'];

        return (new LaraFcm)
        ->notification(
            (new LaraFcmNotification)
                ->setTitle('New Order')
                ->setBody('You have placed order')
                ->setColor('#f00')
        )
        ->options(
            (new Options)
            ->setTimeToLive(60*20)
            ->setContentAvailable(true)
        )
        ->data(
            (new Data)
            ->addData(['key'=>"value"])
        )
        ->to($tokens)
        ->send();
    }
}