Wallogit.com
2017 © Pedro Peláez
This is to send push notification with firebase cloud messaging for Laravel. Tested in Laravel 5.2.*
** Caveat: This package is not maintained anymore., (*1)
This Package is to enable sending push notifications to devices with firebase cloud messaging. Tested in Laravel 5.1 and 5.2, (*2)
To install the package simply run this command in Laravel project root folder, (*3)
php composer.phar require saikatdutta1991/firebasecloudmessaging:1.0.0
add the service provider, (*4)
'providers' => [ /* * Laravel Framework Service Providers... */ ..... ..... \Saikat\FirebaseCloudMessaging\FCMServiceProvider::class ]
to config\app.php providers array, (*5)
Alias the PushManager class adding it to the aliases array in the config/app.php file., (*6)
'aliases' => [
...
'PushManager' => Saikat\FirebaseCloudMessaging\PushManager::class
]
But the alias is not mandatory., (*7)
publish the package config to your laravel config folder with executing command, (*8)
php artisan vendor:publish --tag="config"
before executing this command add the service provider first, (*9)
add the fcm server_key in the config/firebase_cloud_messaging.php file, (*10)
return [
"server_key" => env('FIREBASE_CLOUD_MESSAGING_SERVER_KEY'),
"fcm_push_url" => env("FIREBASE_CLOUD_MESSAING_URL")
];
fcm_push_url is not required already included in package. If want to override then add it, (*11)
If added in alias then use it in controller, (*12)
use PushManager;
IF not added in alias then use it in controller, (*13)
use Saikat\FirebaseCloudMessaging\PushManager;
class YourController extends Controller
{
public function __construct(PushManager $pushManager)
{
$this->pushManager = $pushManager;
}
If want to use without injecting then, (*14)
class YourController extends Controller
{
public function sendPushNotification()
{
$pushManager = app('PushManager'); // this will keep the PushManager instance singleton
}
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use PushManager;
class Controller extends Controller
{
public function __construct(PushManager $pushManager)
{
$this->pushManager = $pushManager;
}
public function sendPushNotification()
{
$response = $this->pushManager
->setTitle('Test Title')
->setBody('Test Body')
->setIcon('icon url')
->setClickAction('https://www.google.com')
->setCustomPayload(['custom_data' => 'custom_data_array'])
->setPriority(PushNotification::HIGH)
->setContentAvailable(true)
->setDeviceTokens('--------------------') // this can be an array or string
->push();
dd( $response );
}
}
->push(PushManager::STDCLASS)
PushManager::STDCLASS, PushManager::ARRY, PushManager::RAW, (*15)
by default response is PushManager::RAW set, (*16)
$this->pushManager->getLastErrorCode() //if no error then 0
and, (*17)
$this->pushManager->getLastErrorMessage() // if no error then ""
Now only raw response works proper. Json and stdClas response doesn't work for all types of fcm response. Returns null if failed to make json decode., (*18)
I will make the send push notification send asynchronous so that no wait for the response, (*19)