tonini46 / laravel-fcm-notification
Laravel FCM (Firebase Cloud Messaging) Notification Channel
Installs: 18
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tonini46/laravel-fcm-notification
Requires
- php: ^7.1.3|^8.0
- guzzlehttp/guzzle: *
- illuminate/config: *
- illuminate/notifications: *
- illuminate/queue: *
- illuminate/support: *
- laravel/framework: ^5.7|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ~1.0
- phpunit/phpunit: ~6.0
README
Laravel FCM (Firebase Cloud Messaging) Notification Channel
Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.5+ required.
This package works only with Legacy HTTP Server Protocol
Install
This package can be installed through Composer.
composer require tonini46/laravel-fcm-notification
If installing on < Laravel 5.5 then add the service provider:
// config/app.php 'providers' => [ ... tonini46\FCM\FcmNotificationServiceProvider::class, ... ];
Add your Firebase API Key in config/services.php.
return [ ... ... /* * Add the Firebase API key */ 'fcm' => [ 'key' => env('FCM_SECRET_KEY') ] ];
Example Usage
Use Artisan to create a notification:
php artisan make:notification SomeNotification
Return [fcm] in the public function via($notifiable) method of your notification:
public function via($notifiable) { return ['fcm']; }
Add the method public function toFcm($notifiable) to your notification, and return an instance of FcmMessage:
use tonini46\FCM\FcmMessage; ... public function toFcm($notifiable) { $message = new FcmMessage(); $message->content([ 'title' => 'Foo', 'body' => 'Bar', 'sound' => '', // Optional 'icon' => '', // Optional 'click_action' => '' // Optional ])->data([ 'param1' => 'baz' // Optional ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'. return $message; }
When sending to specific device, make sure your notifiable entity has routeNotificationForFcm method defined:
/** * Route notifications for the FCM channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForFcm($notification) { return $this->device_token; }
When sending to a topic, you may define so within the toFcm method in the notification:
use tonini46\FCM\FcmMessage; ... public function toFcm($notifiable) { $message = new FcmMessage(); $message->to('the-topic', $recipientIsTopic = true) ->content([...]) ->data([...]); return $message; }
Or when sending with a condition:
use tonini46\FCM\FcmMessage; ... public function toFcm($notifiable) { $message = new FcmMessage(); $message->contentAvailable(true) ->priority('high') ->condition("'user_".$notifiable->id."' in topics") ->data([...]); return $message; }
You may provide optional headers or override the request headers using setHeaders():
use tonini46\FCM\FcmMessage; ... public function toFcm($notifiable) { $message = new FcmMessage(); $message->setHeaders([ 'project_id' => "48542497347" // FCM sender_id ])->content([ 'title' => 'Foo', 'body' => 'Bar', 'sound' => '', // Optional 'icon' => '', // Optional 'click_action' => '' // Optional ])->data([ 'param1' => 'baz' // Optional ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'. return $message; }
Interpreting a Response
To process any laravel notification channel response check Laravel Notification Events
This channel return a json array response:
{
"multicast_id": "number",
"success": "number",
"failure": "number",
"canonical_ids": "number",
"results": "array"
}
Check FCM Legacy HTTP Server Protocol for response interpreting documentation.
License
The MIT License (MIT). Please see License File for more information.