public function sendPushNotification(Notification $notification)
 {
     $sm = $this->getServiceLocator();
     $objectManager = $sm->get('Doctrine\\ORM\\EntityManager');
     $config = $sm->get('Config');
     if (!isset($config['urban-airship'])) {
         throw new \Exception('Urban Airship configuration is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path.');
     } else {
         if (!isset($config['urban-airship']['api-key']) || $config['urban-airship']['api-key']) {
             throw new \Exception('Urban Airship API key is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path and that all variables are set correctly.');
         } else {
             if (!isset($config['urban-airship']['api-secret']) || $config['urban-airship']['api-secret']) {
                 throw new \Exception('Urban Airship API secret is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path and that all variables are set correctly.');
             }
         }
     }
     UALog::setLogHandlers(array(new StreamHandler('logs' . DIRECTORY_SEPARATOR . 'urban_airship.' . date('Y-m-d') . '.log', Logger::DEBUG)));
     $airship = new Airship($config['urban-airship']['api-key'], $config['urban-airship']['api-secret']);
     try {
         $response = $airship->push()->setAudience(P\deviceToken($device->getDeviceToken()))->setNotification(P\notification($message))->setDeviceTypes(P\deviceTypes($device->getDeviceType()))->send();
         if ($response->ok) {
             $notification->setStatus('sent');
             $notification->setOperationId($response->operation_id);
             $notification->setPushIds($response->push_ids);
         } else {
             $notification->setStatus('failed');
         }
         $objectManager->persist($notification);
         $objectManager->flush();
     } catch (AirshipException $e) {
         $error = $this->logError($e->getCode(), $e->getMessage(), __FILE__, __LINE__, $e->getTrace());
         $notification->setStatus('failed');
         $notification->setError($error);
         $objectManager->persist($notification);
         $objectManager->flush();
         throw $e;
     }
 }
Ejemplo n.º 2
0
<?php

require_once 'vendor/autoload.php';
use UrbanAirship\Airship;
use UrbanAirship\UALog;
use UrbanAirship\Push as P;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
UALog::setLogHandlers(array(new StreamHandler("php://stdout", Logger::INFO)));
$airship = new Airship("key", "secret");
$response = $airship->push()->setAudience(P\all)->setNotification(P\notification("Hello from PHP"))->setDeviceTypes(P\all)->send();