public function testSimplePushRequest() { $response = new StdClass(); $response->code = 202; $response->raw_headers = array(); $response->raw_body = "{\"push_ids\": [\"41742a47-bd36-4a0e-8ce2-866cd8f3b1b5\"]}"; $airship = $this->getMock('Airship', array('request', 'buildUrl')); $airship->expects($this->any())->method('request')->will($this->returnValue($response)); $push = new P\PushRequest($airship); $push = $push->setAudience(P\all)->setNotification(P\notification("Hello"))->setDeviceTypes(P\all)->setOptions(array()); $response = $push->send(); }
public function testSimpleAlert() { $this->assertEquals(P\notification("Hi"), array("alert" => "Hi")); }
/** * Private method to send push or broadcast. * * @param string $alert * @param string $tag * */ function _send_broadcast_or_push($alert, $tag, $url = false, $disable_sound = false) { // Strip escape slashes, otherwise double escaping would happen $alert = html_entity_decode(stripcslashes(strip_tags($alert))); // Grab an instance of PushRequest $response = $this->airship->push(); // If tag is broadcast use Push\all const, otherwise set audience to tag $audience = $tag == 'broadcast' ? P\all : P\tag($tag); $response->setAudience($audience); // Sound part of ios push if (!$disable_sound && isset($this->sounds["blimply_sound_{$tag}"]) && !empty($this->sounds["blimply_sound_{$tag}"])) { $sound = $this->sounds["blimply_sound_{$tag}"]; } elseif (!$disable_sound) { $sound = 'default'; } else { $sound = null; } // Set extra payload arguments, for now it's just the url $extra = $url ? array('url' => $url) : null; // iOS and Android specific parts of payload $ios = P\ios($alert, '+1', $sound, false, $extra); $android = P\android($alert, null, null, null, $extra); // TODO: Windows and Blackberry payloads (HA!) // Payload filter (allows to workaround quirks of UA API if any and/or fine tuning of payload data) $payload = apply_filters('blimply_payload_override', P\notification($alert, array('ios' => $ios, 'android' => $android))); $response->setNotification($payload)->setDeviceTypes(P\all); do_action('blimply_before_send_push', $payload); try { $response->send(); } catch (\Exception $e) { return new WP_Error($e->getCode(), $e->getMessage()); } return $response; }
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; } }
<?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();
public function testSimpleScheduledPushRequest() { $response = new StdClass(); $response->code = 201; $response->raw_headers = array(); $response->raw_body = "{\"schedule_urls\": [\"https://go.urbanairship.com/api/schedules/41742a47-bd36-4a0e-8ce2-866cd8f3b1b5\"]}"; $airship = $this->getMock('Airship', array('request', 'buildUrl')); $airship->expects($this->any())->method('request')->will($this->returnValue($response)); $push = new P\PushRequest($airship); $push = $push->setAudience(P\all)->setNotification(P\notification("Hello"))->setDeviceTypes(P\all)->setOptions(array()); $sched = new P\ScheduledPushRequest($airship); $sched = $sched->setName("A schedule")->setPush($push)->setSchedule(P\scheduledTime(time() + 15)); $response = $sched->send(); }