/**
  * __toString
  *
  * @return string
  */
 public function __toString()
 {
     return sprintf('Error sending push: "%s", with code status: %d. Message identifier: %d.%s', $this->getMessage(), $this->statusCode, $this->identifier, $this->messageObject ? ' Device token: ' . $this->messageObject->getDeviceToken() : '');
 }
 /**
  * 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;
 }