public function sendPushNotification(PushNotification $notification) { $app = $this->getSetting('App'); $devices = array_filter(explode(',', $this->getSetting('Devices')), 'strlen'); if (!$app) { throw new PushException('No application was selected.'); } if (!isset(self::$applications[$app])) { throw new PushException(sprintf('No settings were provided for application "%s"', $app)); } if (!$devices) { throw new PushException('At least one device type must be selected to send to.'); } $user = self::$applications[$app]['key']; $pass = self::$applications[$app]['secret']; $request = function ($url, $payload) use($user, $pass) { $client = new Zend\Http\Client($url); $client->setAuth($user, $pass); $client->setHeaders('Content-Type: application/json'); $client->setRawData(json_encode($payload), 'application/json'); try { $response = $client->request('POST'); } catch (Zend\Http\Client_Exception $e) { throw new PushException($e->getMessage(), $e->getCode(), $e); } if ($response->isError()) { throw new PushException($response->getBody(), $response->getStatus()); } }; // Use the V1 API for sending to Android, Blackberry and iOS. if (array_intersect($devices, array(self::ANDROID, self::BLACKBERRY, self::IOS))) { $body = array(); if (in_array(self::ANDROID, $devices)) { $body['android'] = array('alert' => $notification->Content); } if (in_array(self::BLACKBERRY, $devices)) { $body['blackberry'] = array('content-type' => 'text/plain', 'body' => $notification->Content); } if (in_array(self::IOS, $devices)) { $body['aps'] = array('badge' => $this->getSetting('Badge') == 'inc' ? '+1' : $this->getSetting('Badge'), 'alert' => $notification->Content, 'sound' => $this->getSetting('Sound')); } $request(self::V1_API_URL . '/broadcast/', $body); } // Use the V2 API for sending to Windows. if (array_intersect($devices, array(self::MPNS, self::WNS))) { $types = array(); if (in_array(self::MPNS, $devices)) { $types[] = 'mpns'; } if (in_array(self::WNS, $devices)) { $types[] = 'wns'; } $request(self::V2_API_URL . '/broadcast/', array('notification' => array('alert' => $notification->Content), 'device_types' => $types)); } }