private function makeRequest(Message $message, array $registrationIds) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, self::GCM_ENDPOINT); if ($this->certificatePath != null) { curl_setopt($ch, CURLOPT_CAINFO, $this->certificatePath); } curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $this->key)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $message->build($registrationIds)); $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status == 400) { throw new InvalidRequestException($status, 'Check that the JSON message is properly formatted and contains valid fields.'); } else { if ($status == 401) { throw new InvalidRequestException($status, 'The sender account used to send a message could not be authenticated.'); } else { if ($status == 500) { throw new InvalidRequestException($status, 'The server encountered an error while trying to process the request.'); } else { if ($status == 503) { throw new InvalidRequestException($status, 'The server could not process the request in time.'); } else { if ($status != 200) { throw new InvalidRequestException($status, $response); } } } } } $response = json_decode($response, true); $success = $response[self::SUCCESS]; $failure = $response[self::FAILURE]; $canonicalIds = $response[self::CANONICAL_IDS]; $multicastId = $response[self::MULTICAST_ID]; $multicastResult = new MulticastResult($success, $failure, $canonicalIds, $multicastId); if (isset($response[self::RESULTS])) { $individualResults = $response[self::RESULTS]; foreach ($individualResults as $singleResult) { $messageId = isset($singleResult[self::MESSAGE_ID]) ? $singleResult[self::MESSAGE_ID] : null; $canonicalRegId = isset($singleResult[self::REGISTRATION_ID]) ? $singleResult[self::REGISTRATION_ID] : null; $error = isset($singleResult[self::ERROR]) ? $singleResult[self::ERROR] : null; $result = new Result(); $result->setMessageId($messageId); $result->setCanonicalRegistrationId($canonicalRegId); $result->setErrorCode($error); $multicastResult->addResult($result); } } return $multicastResult; }
/** * Sends a message without retrying in case of service unavailability. See * sendMulti() for more info. * * @return {@literal true} if the message was sent successfully, * {@literal false} if it failed but could be retried. * * @throws \InvalidArgumentException if registrationIds is {@literal null} or * empty. * @throws InvalidRequestException if GCM didn't returned a 200 status. * @throws \Exception if message could not be sent or received. */ public function sendNoRetryMulti(Message $message, array $registrationIds) { if (is_null($registrationIds) || count($registrationIds) == 0) { throw new \InvalidArgumentException('registrationIds cannot be null or empty'); } $request = array(); if ($message->getTimeToLive() != -1) { $request[Constants::$PARAM_TIME_TO_LIVE] = $message->getTimeToLive(); } if ($message->getCollapseKey() != '') { $request[Constants::$PARAM_COLLAPSE_KEY] = $message->getCollapseKey(); } if ($message->getDelayWhileIdle() != '') { $request[Constants::$PARAM_DELAY_WHILE_IDLE] = $message->getDelayWhileIdle(); } $request[Constants::$JSON_REGISTRATION_IDS] = $registrationIds; if (!is_null($message->getData()) && count($message->getData()) > 0) { $request[Constants::$JSON_PAYLOAD] = $message->getData(); } $request = json_encode($request); $headers = array('Content-Type: application/json', 'Authorization: key=' . $this->key); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, Constants::$GCM_SEND_ENDPOINT); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status != 200) { throw new InvalidRequestException($status, $response); } $response = json_decode($response, true); $success = $response[Constants::$JSON_SUCCESS]; $failure = $response[Constants::$JSON_FAILURE]; $canonicalIds = $response[Constants::$JSON_CANONICAL_IDS]; $multicastId = $response[Constants::$JSON_MULTICAST_ID]; $multicastResult = new MulticastResult($success, $failure, $canonicalIds, $multicastId); if (isset($response[Constants::$JSON_RESULTS])) { $individualResults = $response[Constants::$JSON_RESULTS]; foreach ($individualResults as $singleResult) { $messageId = isset($singleResult[Constants::$JSON_MESSAGE_ID]) ? $singleResult[Constants::$JSON_MESSAGE_ID] : null; $canonicalRegId = isset($singleResult[Constants::$TOKEN_CANONICAL_REG_ID]) ? $singleResult[Constants::$TOKEN_CANONICAL_REG_ID] : null; $error = isset($singleResult[Constants::$JSON_ERROR]) ? $singleResult[Constants::$JSON_ERROR] : null; $result = new Result(); $result->setMessageId($messageId); $result->setCanonicalRegistrationId($canonicalRegId); $result->setErrorCode($error); $multicastResult->addResult($result); } } return $multicastResult; }
public function testSetsMessageId() { $result = new Result(); $result->setMessageId('message-id'); $this->assertEquals('message-id', $result->getMessageId()); }