Skip to content
Snippets Groups Projects
Select Git revision
  • 00ed6a769c8ddb61ddc6648cefd22b64e869ee50
  • master default
2 results

NotificationOnDeviceRepository.php

Blame
  • NotificationController.php 4.12 KiB
    <?php
    /**
     * Created by PhpStorm.
     * User: jeremyguiselin
     * Date: 23/09/2016
     * Time: 08:22
     */
    
    namespace BackendBundle\Controller;
    
    use BackendBundle\Entity\Notification;
    use Doctrine\ORM\Tools\Pagination\Paginator;
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\BadResponseException;
    use GuzzleHttp\Exception\ClientException;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    
    class NotificationController extends Controller
    {
    
        const NOTIFICATION_API_URL = 'https://fcm.googleapis.com/fcm/send';
        const SERVER_KEY = "AIzaSyCFdGq-rjzznFOEzDifZGsRoeWT8LFAc9s";
        const SENDER_ID = "1006878207346";
        const DEFAULT_PAGE_RESULT = 20;
    
    
    
        /**
         * @param Request $request
         * @return Response
         *
         * @Route("/admin/notification/create")
         */
        public function createAction(Request $request)
        {
    
            $notification = new Notification();
    
            $form = $this->createForm('BackendBundle\\Form\\NotificationType', $notification);
    
            if ($request->getMethod() === Request::METHOD_POST) {
                $form->handleRequest($request);
    
                if ($form->isValid()) {
                    $message = [
                        'notification' => [
                            'title' => $notification->getTitle(),
                            'body' => $notification->getContent()
                        ],
                        'to' => 'cNgyx3ZgOX4:APA91bF5eigDuJLjj8wI8SC4l5KIHATuxi-2G5whpWI1n1jWpWWwRAwbEXec4fQ2S7HW8EdUPaREXJ5fVd_TZV8rZq_eIJIEhEadeH9wpsbYmUUM7E8H8Y0Qst9KmBuHWodCFFJPJ0vh'
                    ];
    
                    $client = new Client();
                    $response = $client->request('POST', self::NOTIFICATION_API_URL, [
                        'body' => json_encode($message),
                        'headers'=> [
                            'Authorization' => 'key='.self::SERVER_KEY,
                            'Content-Type'  => 'application/json',
                        ]
                    ]);
    
                    $response = (array) \GuzzleHttp\json_decode($response->getBody()->getContents());
    
                    if (!$response['failure']) {
                        $notification->setDate(new \DateTime());
                        $devices = $this->getDoctrine()->getRepository('BackendBundle:Device')->findAll();
                        $em = $this->getDoctrine()->getEntityManager();