|
1 | | -# laravel-firebase-batch-messaging |
2 | | -Laravel package for Firebase cloud batch messaging API. |
| 1 | +# Batch Messaging using Firebase FCM API |
| 2 | +This is based on [Firebase Doc](https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-multiple-devices.). Usually, using [Channel](https://laravel.com/docs/8.x/notifications#specifying-delivery-channels) 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. |
| 3 | + |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install using composer : |
| 8 | +```code |
| 9 | +composer require quantomtech/laravel-firebase-batch-messaging |
| 10 | +``` |
| 11 | + |
| 12 | +Publish config File : |
| 13 | +```code |
| 14 | +php artisan vendor:publish --provider="Quantomtech\LaravelFirebaseBatchMessaging\Providers\FCMBatchServiceProvider" |
| 15 | +``` |
| 16 | + |
| 17 | +## Setup Credentials |
| 18 | + |
| 19 | +Define Google Service Account JSON path inside .env: |
| 20 | +```code |
| 21 | +FCMB_SERVICE_JSON_BASE_PATH='/var/www/my-laravel-project/service-app-firebase.json' |
| 22 | +``` |
| 23 | + |
| 24 | +Optional configuration inside .env : |
| 25 | +```code |
| 26 | +# Is the filename for your notification sound file. |
| 27 | +FCMB_SOUND="my_custom_noti_sound.wav' |
| 28 | +
|
| 29 | +# Is the path to generate temporary file for the package |
| 30 | +FCMB_TEMP_FOLDER= |
| 31 | +``` |
| 32 | + |
| 33 | +## Sample Use |
| 34 | + |
| 35 | +Considering 1000 users, 2 batch jobs are queued as Firebase only allow 500 messages max per API call. |
| 36 | + |
| 37 | +SendFCMJob.php to queue tasks : |
| 38 | +```code |
| 39 | +<?php |
| 40 | +
|
| 41 | +class SendNotificationReferralJob implements ShouldQueue |
| 42 | +{ |
| 43 | + use Dispatchable, |
| 44 | + InteractsWithQueue, |
| 45 | + Queueable, |
| 46 | + SerializesModels; |
| 47 | + |
| 48 | + protected $userIds; |
| 49 | + protected $title; |
| 50 | + protected $message; |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a new job instance. |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function __construct(array $userIds) |
| 58 | + { |
| 59 | + $this->userIds = $userIds; |
| 60 | + $this->title = trans("notification.share_title"); |
| 61 | + $this->message = trans("notification.share_body"); |
| 62 | + } |
| 63 | +
|
| 64 | + /** |
| 65 | + * Execute the job. |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function handle() |
| 70 | + { |
| 71 | + try{ |
| 72 | + User::whereIn('id', $this->userIds) |
| 73 | + ->select(['fcm_token', 'apns_token']) |
| 74 | + ->each(function($user) |
| 75 | + { |
| 76 | + $this->batchService->addPayload( |
| 77 | + $user->fcm_token, |
| 78 | + $this->title, |
| 79 | + $this->message, |
| 80 | + [ |
| 81 | + "key" => "foo", |
| 82 | + ], |
| 83 | + (! is_null($user->apns_token) |
| 84 | + ); |
| 85 | + }); |
| 86 | +
|
| 87 | + $this->batchService->send(); |
| 88 | + } |
| 89 | + catch (Exception $e){ |
| 90 | + // |
| 91 | + } |
| 92 | + } |
| 93 | +
|
| 94 | +``` |
| 95 | + |
| 96 | +Dispatching SendFCMJob in Controller : |
| 97 | + |
| 98 | +```code |
| 99 | +<?php |
| 100 | +
|
| 101 | +use App\Http\Controllers\Controller; |
| 102 | +use App\User; |
| 103 | +use Illuminate\Http\Request; |
| 104 | +
|
| 105 | +
|
| 106 | +class NotificationController extends Controller |
| 107 | +{ |
| 108 | + /** |
| 109 | + * Send notification to users. |
| 110 | + * |
| 111 | + * @param \Illuminate\Http\Request $request |
| 112 | + * @return \Illuminate\Http\Response |
| 113 | + */ |
| 114 | + public function store(Request $request) |
| 115 | + { |
| 116 | + User::query() |
| 117 | + ->select(['id']) |
| 118 | + ->chunkById(500, function($users) { |
| 119 | + SendFCMJob::dispatch($ids); |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
0 commit comments