/**
  * Correlate Message and Result
  *
  * @return array
  */
 protected function correlate()
 {
     $results = $this->results;
     if ($this->message && $results) {
         $ids = $this->message->getRegistrationIds();
         while ($id = array_shift($ids)) {
             $results[$id] = array_shift($results);
         }
     }
     return $results;
 }
 public function testResponse()
 {
     $responseArr = array('results' => array(array('message_id' => '1:234')), 'success' => 1, 'failure' => 0, 'canonical_ids' => 0, 'multicast_id' => '123');
     $response = new Response();
     $response->setResponse($responseArr);
     $this->assertEquals($responseArr, $response->getResponse());
     $this->assertEquals(1, $response->getSuccessCount());
     $this->assertEquals(0, $response->getFailureCount());
     $this->assertEquals(0, $response->getCanonicalCount());
     // test results non correlated
     $expected = array(array('message_id' => '1:234'));
     $this->assertEquals($expected, $response->getResults());
     $expected = array(0 => '1:234');
     $this->assertEquals($expected, $response->getResult(Response::RESULT_MESSAGE_ID));
     $message = new Message();
     $message->setRegistrationIds(array('ABCDEF'));
     $response->setMessage($message);
     $expected = array('ABCDEF' => '1:234');
     $this->assertEquals($expected, $response->getResult(Response::RESULT_MESSAGE_ID));
 }
Exemple #3
0
 /**
  * Build a \ZendService\Google\Gcm\Message from a \HipsterJazzbo\Telegraph\Message
  *
  * @param \HipsterJazzbo\Telegraph\Pushable $pushable
  * @param \HipsterJazzbo\Telegraph\Message  $message
  *
  * @return \ZendService\Google\Gcm\Message
  */
 protected function buildServiceMessage(Pushable $pushable, Message $message)
 {
     $gcmMessage = new GcmMessage();
     $gcmMessage->addRegistrationId($pushable->getToken());
     $gcmMessage->setTitle($message->getTitle());
     $gcmMessage->setBody($message->getBody());
     $gcmMessage->setData($message->getData());
     return $gcmMessage;
 }
 /**
  * Send Message
  *
  * @param Mesage $message
  * @return Response
  * @throws Exception\RuntimeException
  */
 public function send(Message $message)
 {
     $client = $this->getHttpClient();
     $client->setUri(self::SERVER_URI);
     $headers = $client->getRequest()->getHeaders();
     $headers->addHeaderLine('Authorization', 'key=' . $this->getApiKey());
     $response = $client->setHeaders($headers)->setMethod('POST')->setRawBody($message->toJson())->setEncType('application/json')->send();
     switch ($response->getStatusCode()) {
         case 500:
             throw new Exception\RuntimeException('500 Internal Server Error');
             break;
         case 503:
             $exceptionMessage = '503 Server Unavailable';
             if ($retry = $response->getHeaders()->get('Retry-After')) {
                 $exceptionMessage .= '; Retry After: ' . $retry;
             }
             throw new Exception\RuntimeException($exceptionMessage);
             break;
         case 401:
             throw new Exception\RuntimeException('401 Forbidden; Authentication Error');
             break;
         case 400:
             throw new Exception\RuntimeException('400 Bad Request; invalid message');
             break;
     }
     if (!($response = Json::decode($response->getBody(), Json::TYPE_ARRAY))) {
         throw new Exception\RuntimeException('Response body did not contain a valid JSON response');
     }
     return new Response($response, $message);
 }
 /**
  * Get service message from origin.
  *
  * @param array                                          $tokens  Tokens
  * @param \Sly\NotificationPusher\Model\MessageInterface $message Message
  *
  * @return \ZendService\Google\Gcm\Message
  */
 public function getServiceMessageFromOrigin(array $tokens, MessageInterface $message)
 {
     $data = $message->getOptions();
     $data['message'] = $message->getText();
     $serviceMessage = new ServiceMessage();
     $serviceMessage->setRegistrationIds($tokens);
     $serviceMessage->setData($data);
     $serviceMessage->setCollapseKey($this->getParameter('collapseKey'));
     $serviceMessage->setRestrictedPackageName($this->getParameter('restrictedPackageName'));
     $serviceMessage->setDelayWhileIdle($this->getParameter('delayWhileIdle', false));
     $serviceMessage->setTimeToLive($this->getParameter('ttl', 600));
     $serviceMessage->setDryRun($this->getParameter('dryRun', false));
     return $serviceMessage;
 }