PHP code example of quantomtech / laravel-firebase-batch-messaging

1. Go to this page and download the library: Download quantomtech/laravel-firebase-batch-messaging 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/ */

    

quantomtech / laravel-firebase-batch-messaging example snippets

code
php artisan vendor:publish --provider="Quantomtech\LaravelFirebaseBatchMessaging\Providers\FCMBatchServiceProvider"
code


class SendNotificationReferralJob implements ShouldQueue
{
    use Dispatchable, 
        InteractsWithQueue, 
        Queueable, 
        SerializesModels;
    
    protected $userIds;
    protected $title;
    protected $message;
        
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(array $userIds)
    {
        $this->userIds  = $userIds;
        $this->title    = trans("notification.share_title");
        $this->message  = trans("notification.share_body");
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            User::whereIn('id', $this->userIds)
                ->select(['fcm_token', 'apns_token'])
                ->each(function($user)
                {
                    $this->batchService->addPayload(
                        $user->fcm_token, 
                        $this->title, 
                        $this->message, 
                        [
                            "key" => "foo",
                        ],
                        (! is_null($user->apns_token)
                    );
                });

            $this->batchService->send();
        }
        catch (Exception $e){
            //
        }
    }

code


use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;


class NotificationController extends Controller
{
    /**
     * Send notification to users.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        User::query()
          ->select(['id'])
          ->chunkById(500, function($users) {
              SendFCMJob::dispatch($ids);
          });
    }
}