定制 quantomtech/laravel-firebase-batch-messaging 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

quantomtech/laravel-firebase-batch-messaging

最新稳定版本:1.0.3

Composer 安装命令:

composer require quantomtech/laravel-firebase-batch-messaging

包简介

Laravel package for batch Firebase cloud messaging API

README 文档

README

This is based on Firebase Doc. Usually, using Channel will result in one API call per one message although queued. Considering bulk users with more than thousands, this largely overwhelm API calls. This package implement simple method to send batch max 500 messages per API call.

Installation

Install using composer :

composer require quantomtech/laravel-firebase-batch-messaging

Publish config File :

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

Setup Credentials

Define Google Service Account JSON path inside .env:

FCMB_SERVICE_JSON_BASE_PATH='/var/www/my-laravel-project/service-app-firebase.json'

Optional configuration inside .env :

# Is the filename for your notification sound file.
FCMB_SOUND="my_custom_noti_sound.wav'

# Is the path to generate temporary file for the package
FCMB_TEMP_FOLDER=

Sample Use

Considering 1000 users, 2 batch jobs are queued as Firebase only allow 500 messages max per API call.

SendFCMJob.php to queue tasks :

<?php

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){
            //
        }
    }

Dispatching SendFCMJob in Controller :

<?php

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

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-15