<?php
use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;
use sngrl\PhpFirebaseCloudMessaging\Notification;

class FcmHelper
{
    static public function sendNotif($token, $title, $body, $data, $clickAction = null){
        $client = new Client();
        $client->setApiKey(getenv("FCM_SERVER_KEY"));
        $client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
        $notif = new Notification($title, $body);
        // if( $clickAction !== null ){
        //     $notif->setClickAction($clickAction);
        // }
        $message = new Message();
        $message->setPriority('high');
        $message->addRecipient(new Device($token));
        $message
            ->setNotification($notif)
            ->setData($data);
        $response = $client->send($message);
        return $response;
    }

    static public function sendNotifBg($token, $title, $body, $data, $clickAction = null)
    {
        /**
         * #1100 - pengiriman email/notif tidak menggunakan beanstlakd lagi
         */
//        $bg = new BackgroundJob(
//            [
//                "host" => getenv("BEANSTALK_SERVER"),
//                "port" => getenv("BEANSTALK_PORT")
//            ]
//        );
//
//        $bg->put('FcmHelper', '', 'sendNotif', [
//            $token,
//            $title,
//            $body,
//            $data,
//            $clickAction
//        ],
//            1);
        return self::sendNotif($token, $title, $body, $data, $clickAction);
    }

    /**
     * @throws CustomErrorException
     */
    static public function logNotif($umId, $token, $title, $body, $data) {
        LogNotification::insert($umId, $token, $title, $body, $data);
    }
}
?>
