<?php
namespace App\EventSubscriber;
use Pimcore\Mail;
use Pimcore\Model\DataObject\News;
use Pimcore\Event\Model\DataObjectEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NewsEventListener
{
// public static function getSubscribedEvents()
// {
// return [
// DataObjectEvent::class => 'onObjectPublish',
// ];
// }
public function onObjectPublish(DataObjectEvent $event)
{
$object = $event->getObject();
if (($object instanceof News) && ($object->getPublishNewsNotification() == true) && ($object->isPublished(true))) {
$name = $object->getTitle();
$url = 'https://fcm.googleapis.com/fcm/send';
$arrNotification = array();
$arrNotification["body"] = "Test Notification for API";
$arrNotification["title"] = "Test Notification";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1;
$fields = array(
'to' => 'fh-jPL3DE7s:APA91bHDIxgp0nLLX7PeCur5MWD9VIrvSpMbwy-2nFZ553zoNKZggIUFknV7EOvzyRmFAs8cx8SoaMkrY6fvcFa0M-Pfj-RTvruhPnH7b3X9E_VKLEirV-8jf2zX7IQESkkr0RRL4UWJ',
'notification' => $arrNotification
);
// Firebase API Key
$headers = array('Authorization:key=AAAAu0kxbzQ:APA91bHiJohDMyPSa3jpkQJFOF-YFSUAK4n5ZQrQbd13vwtECRdRvAk-9G7JO7EyBK3luOq34zShwJp2tl8_ONqdEKqEvhzBKRGG32Uq-zImsjNDwzpEWUnABAY_-3rcsnX-Tl17QA-l', 'Content-Type:application/json');
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
}
}