/** * Get invalid devices * * @return array|Device[] * * @throws Exception\ConnectionUndefinedException */ public function getInvalidDevices() { if (!$this->connection) { throw new Exception\ConnectionUndefinedException(); } if (!$this->connection->is()) { if ($this->logger) { $this->logger->debug('Create feedback connection...'); } $this->connection->create(); } $data = $this->connection->read(-1); $this->connection->close(); $feedback = array(); if ($data) { foreach (str_split($data, 38) as $deviceData) { $feedback[] = new Device($deviceData); } } if ($this->logger) { $this->logger->info(sprintf('%d device tokens received from feedback service.', count($feedback))); } return $feedback; }
/** * Set message to iOS devices * * @param MessageInterface $message * * @return bool * * @throws SendException * @throws Exception\PayloadFactoryUndefinedException * @throws Exception\ConnectionUndefinedException * @throws Exception\DeviceTokenNotFoundException */ public function send(MessageInterface $message) { if (!$this->payloadFactory) { throw new Exception\PayloadFactoryUndefinedException(); } if (!$this->connection) { throw new Exception\ConnectionUndefinedException(); } if (!$message->getDeviceToken()) { throw new Exception\DeviceTokenNotFoundException(); } if (!$this->connection->is()) { if ($this->logger) { $this->logger->debug('Connect...'); } $this->connection->connect(); } try { // Send payload data to apns server $response = $this->sendPayload($message); } catch (SendException $error) { if ($this->eventDispatcher) { // Call to event: Error send message $event = new SendMessageErrorEvent($message, $error); $this->eventDispatcher->dispatch(NotificationEvents::SEND_MESSAGE_ERROR, $event); } if ($this->logger) { // Write error to log $this->logger->error((string) $error); $this->logger->debug('Close connection...'); } $this->connection->close(); throw $error; } if ($this->eventDispatcher) { // Call to event: Complete send message $event = new SendMessageCompleteEvent($message); $this->eventDispatcher->dispatch(NotificationEvents::SEND_MESSAGE_COMPLETE, $event); } if ($this->logger) { $this->logger->info(sprintf('Success send notification to device "%s" by message identifier "%s".', $message->getDeviceToken(), $message->getIdentifier())); } return $response; }