src/EventSubscriber/NewsEventListener.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Pimcore\Mail;
  4. use Pimcore\Model\DataObject\News;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class NewsEventListener
  8. {
  9.     // public static function getSubscribedEvents()
  10.     // {
  11.     //     return [
  12.     //         DataObjectEvent::class => 'onObjectPublish',
  13.     //     ];
  14.     // }
  15.     public function onObjectPublish(DataObjectEvent $event)
  16.     {
  17.         $object $event->getObject();
  18.         if (($object instanceof News) && ($object->getPublishNewsNotification() == true) && ($object->isPublished(true))) {
  19.             $name $object->getTitle();
  20.             $url 'https://fcm.googleapis.com/fcm/send';
  21.             $arrNotification = array();
  22.             $arrNotification["body"] = "Test Notification for API";
  23.             $arrNotification["title"] = "Test Notification";
  24.             $arrNotification["sound"] = "default";
  25.             $arrNotification["type"] = 1;
  26.             $fields = array(
  27.                 'to' => 'fh-jPL3DE7s:APA91bHDIxgp0nLLX7PeCur5MWD9VIrvSpMbwy-2nFZ553zoNKZggIUFknV7EOvzyRmFAs8cx8SoaMkrY6fvcFa0M-Pfj-RTvruhPnH7b3X9E_VKLEirV-8jf2zX7IQESkkr0RRL4UWJ',
  28.                 'notification' => $arrNotification
  29.             );
  30.             // Firebase API Key
  31.             $headers = array('Authorization:key=AAAAu0kxbzQ:APA91bHiJohDMyPSa3jpkQJFOF-YFSUAK4n5ZQrQbd13vwtECRdRvAk-9G7JO7EyBK3luOq34zShwJp2tl8_ONqdEKqEvhzBKRGG32Uq-zImsjNDwzpEWUnABAY_-3rcsnX-Tl17QA-l''Content-Type:application/json');
  32.             // Open connection
  33.             $ch curl_init();
  34.             // Set the url, number of POST vars, POST data
  35.             curl_setopt($chCURLOPT_URL$url);
  36.             curl_setopt($chCURLOPT_POSTtrue);
  37.             curl_setopt($chCURLOPT_HTTPHEADER$headers);
  38.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  39.             // Disabling SSL Certificate support temporarly
  40.             curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  41.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($fields));
  42.             $result curl_exec($ch);
  43.             if ($result === FALSE) {
  44.                 die('Curl failed: ' curl_error($ch));
  45.             }
  46.             curl_close($ch);
  47.         }
  48.     }
  49. }