Exemple #1
0
 /**
  * @param Message $message
  *
  * @return bool
  */
 public function write(Message $message)
 {
     $attempt = 1;
     $content = $message->getPayload();
     $contentLength = strlen($content);
     try {
         while ((int) $this->stream->write($content) !== $contentLength && $attempt++ < self::RETRY) {
             Sleep::millisecond(self::RETRY_INTERVAL);
         }
         $success = $attempt < self::RETRY;
     } catch (\Exception $e) {
         $this->logger->warning('An error occurred writing to APNS stream', array('error_message' => $e->getMessage()));
         $success = false;
     }
     return $success;
 }
Exemple #2
0
 /**
  * @param string         $content
  * @param \ArrayIterator $queue
  *
  * @return \C2iS\ApnsSender\Model\MessageError|bool
  */
 public function process($content, \ArrayIterator $queue)
 {
     while ($queue->valid()) {
         $key = $queue->key();
         $token = $queue->current();
         $message = MessageFactory::createMessage($key, $token, $content);
         if (!$this->streamHandler->write($message)) {
             $streamError = $this->streamHandler->readError();
             $messageError = MessageFactory::createError($queue->getArrayCopy(), $streamError, $key, $token);
             $this->logger->info('Error sending notification to token', array('current_key' => $key, 'current_token' => $token, 'error_code' => $messageError->getErrorCode(), 'error_message' => $messageError->getErrorMessage(), 'error_key' => $messageError->getCustomIdentifier(), 'error_token' => $messageError->getToken()));
             return $messageError;
         }
         $queue->next();
         Sleep::millisecond(self::SEND_INTERVAL);
     }
     Sleep::millisecond(self::END_WAIT);
     $streamError = $this->streamHandler->readError();
     if ($streamError) {
         $messageError = MessageFactory::createError($queue->getArrayCopy(), $streamError);
         $this->logger->info('Error sending notification to token', array('key' => $messageError->getCustomIdentifier(), 'token' => $messageError->getToken(), 'error_code' => $messageError->getErrorCode(), 'error_message' => $messageError->getErrorMessage()));
         return $messageError;
     }
     return true;
 }
Exemple #3
0
 /**
  * Returns the open socket to APNS or throws an exception if the socket is not initialized and the connection fails.
  * If the connection fails, retries up to self::RETRY times.
  *
  * @return resource
  * @throws \C2iS\ApnsSender\Exception\ConnectionFailedException
  */
 public function getApns()
 {
     if (!$this->apns) {
         $attempt = 1;
         while (!($apns = $this->createApns()) && $attempt++ < self::RETRY) {
             Sleep::millisecond(self::RETRY_INTERVAL);
         }
         if ($apns) {
             $this->apns = $apns;
             stream_set_blocking($apns, 0);
         } else {
             throw new ConnectionFailedException(sprintf('Impossible to initialize connexion APNS after %s attempts.', self::RETRY));
         }
     }
     return $this->apns;
 }