venturo/laravel-fcm

push notification async with laravel and fcm

1.0.4 2023-11-01 07:09 UTC

This package is auto-updated.

Last update: 2024-04-30 00:51:39 UTC


README

Package Laravel FCM (asynchronous)

How to install Venturo Laravel FCM

composer require venturo/laravel-fcm

How to use Venturo Laravel FCM

First, you need to setting your .env file

  • Queue Connection (you can use redis or database)

    QUEUE_CONNECTION=database
    FCM_QUEUE=fcm (or you can use something else)
    
  • If your connection is "redis", you need to setup your redis env

    REDIS_CLIENT=predis
    REDIS_HOST=redis-10324.c252****
    REDIS_PASSWORD=Db5E7plgFA****
    REDIS_PORT=10324
    REDIS_QUEUE=hello
    

    Now you can use this packages

Example of Venturo Laravel FCM

use Venturo\LaravelPushNotificationFcm\FCM;

Send Notification

$fcm = new FCM();

$serviceAccount = base_path() . '/resources/auth/auth.json';

$data = [
    'type' => 'token',
    'value' => '<DEVICE_TOKEN / TOPIC>',
    'title' => 'Hello there',
    'body' => 'Test simple notification!',
    'data' => [], //opsional, must be an array
];

$send = $fcm->push($serviceAccount, $data);
// {
//     status: 1,
//     id: "ySy2WlhqTbccLoeU4faQxuWHS6TO3095"
// }
// you can use id to check notification status (pending / finished)

$data can be filled with

  • One Message
    $data = [
      'type' => 'token',
      'value' => '<DEVICE_TOKEN / TOPIC>',
      'title' => 'Hello there',
      'body' => 'Test simple notification!',
      'data' => [], //opsional, must be an array
    ];
    
  • Multiple Messages
    $data = [
      [
          'type' => 'token',
          'value' => '<DEVICE_TOKEN>',
          'title' => 'Hello there',
          'body' => 'Test simple notification!',
          'data' => [], //optional, must be an array
      ],
      [
          'type' => 'token',
          'value' => '<DEVICE_TOKEN>',
          'title' => 'Hello there',
          'body' => 'Test simple notification!',
          'data' => [], //optional, must be an array
      ],
      [
          'type' => 'topic',
          'value' => '<TOPIC>',
          'title' => 'Hello there',
          'body' => 'Test simple notification!',
          'data' => [], //optional, must be an array
      ],
    ];
    

If you want to check notification status (pending / finished), you can use

$fcm = new FCM();

// $id = id you got from send notification function
$inQueue = $fcm->search($id);
// return if status 0 = finished, 1 = pending
// {
//     status: 0
// }

Oh, dont forget to run this command to make queue laravel work

php artisan queue:work --queue=fcm (your FCM_QUEUE env)