CakePHP 4: Firebase Cloud Messaging Component in CakePHP for Push Notification





<?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=AAAAeh0kVhs4ums9A0SYyO2xbCgXvT_jfUUi-0jXs0GfMpZ5DZPqKylqht-TPXGPiqFVj81hBPLL_st9NkjspAarhbfufFHBQN_7'
                                    ]
                    ];
        return $header;
    }

    public function sendSingleNotification() //FCM
    {
        $fcToken = 'e_tHOI1XT3udZvMYS0crNcqD4w6N5g31j6jqid12dGmm4X8Ucm28DjBXDaMJmWMCAxJ3CukpHMjESSsqnJGu08T3sEhspRr';
        $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'];
        $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

Popular posts from this blog

CakePHP 4 : Using Component inside Command ( Shell )

CakePHP: COUNT data and GROUP BY date