Firebase Cloud Messaging library for laravel“s notifications
This library allow send push notifications to Firebase Cloud Messaging through laravel“s notifications., (*1)
It“s possible send notifications to websites, Android and IOS without any other integration., (*2)
Installation
Run the following command from you terminal:, (*3)
bash
composer require "douglasresendemaciel/fcm-laravel-notification:@dev", (*4)
or add this to require section in your composer.json file:, (*5)
"douglasresendemaciel/fcm-laravel-notification", (*6)
then run composer update, (*7)
Once it is installed, you need to register the service provider.
Open up config/app.php and add the following to the providers key., (*8)
'providers' => [
...
DouglasResende\FCM\NotificationServiceProvider::class
...
Usage
First, create your notification class using artisan php artisan make:notification MyNotification, (*9)
Implement your notification:, (*10)
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use DouglasResende\FCM\Messages\FirebaseMessage;
class MyNotification extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['fcm'];
}
public function toFcm($notifiable)
{
return (new FirebaseMessage())->setContent('Test Notification', 'This is a Test');
}
}
Implement on your notifiable, (*11)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class User extends Model {
use Notifiable;
public function routeNotificationForFcm() {
//return a device token, either from the model or from some other place.
return $this->device_token;
}
}
Open up config/broadcasting.php and add the Firebase api key to the connections section:, (*12)
...
'fcm' => [
'key' => env('FCM_API_KEY','YOUR_API_KEY')
]
...
Trigger notification:, (*13)
$User = User::find(1);
//Send Notification
$User->notify(new MyNotification());
References
For more information read the official documentation at https://laravel.com/docs/5.3/notifications, (*14)