src>Command>AppCommand.php
<?php
namespace App\Command;
use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Http\Client;
use Cake\Log\Log;
use Cake\Controller\ComponentRegistry;
use App\Controller\Component\FirebaseComponent;
class AppCommand extends Command
{
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser->addArgument('command', ['help' => 'What your parameter?']);
$parser->addArgument('params', ['help' => 'What your parameter?']);
return $parser;
}
public function execute(Arguments $args, ConsoleIo $io)
{
$command = $args->getArgument('command');
$params = $args->getArgument('params');
if($command == 'send-single-notification')
{
$this->sendSingleNotification($params, $io);
}
if($command == 'send-multiple-notification')
{
$this->sendMultipleNotification($params);
}
}
public function sendSingleNotification($fcTtoken) //FCM
{
//Initiate Firebase Component
$this->Firebase = new FirebaseComponent(new ComponentRegistry());
$fcToken = 'e_tHOI1XT3udZvYvmHn8ztk8hsmhjmBXDaMJ9xhh-No02d9HlMCAu08T3sEhspRr';
$data = [ 'param_1' => 'your_param_1', 'param_2' => 'your_param_2', 'link' => 'https://inigotech.com' ];
$notification = ['title' => 'Payment Update', 'text' => 'Your payment has been VERIFIED'];
//use the firebase component
$this->Firebase->sendSingleNotification($fcToken, $data, $notification);
}
src>Controller>Component>FirebaseComponent.php
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Http\Client;
class FirebaseComponent extends Component
{
public function headers() {
$header = ['headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'key=AAJcn8Fu4ujfUUi-0jXs0GfMDZPqKylqht-TPXGPiqFVj81hBPLL_st9NkjspAarhbfufFHBQN_7'
]
];
return $header;
}
public function sendSingleNotification($fcToken, $data, $notification) //FCM
{
$data = [
'to' => $fcToken,
'data' => $data,
'notification' => $notification
];
$http = new Client();
$response = $http->post(
'https://fcm.googleapis.com/fcm/send',
json_encode($data),
$this->headers()
);
$jsonResult = json_decode($response->getStringBody(), true);
if(isset($jsonResult['success'])){
$success = $jsonResult['failure'];
//$io->out($success);
}
}
}
Comments