當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Result::setCanonicalRegistrationId方法代碼示例

本文整理匯總了PHP中Result::setCanonicalRegistrationId方法的典型用法代碼示例。如果您正苦於以下問題:PHP Result::setCanonicalRegistrationId方法的具體用法?PHP Result::setCanonicalRegistrationId怎麽用?PHP Result::setCanonicalRegistrationId使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Result的用法示例。


在下文中一共展示了Result::setCanonicalRegistrationId方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: makeRequest

 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;
 }
開發者ID:norotaro,項目名稱:php-gcm,代碼行數:54,代碼來源:Sender.php

示例2: sendNoRetryMulti

 /**
  * 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;
 }
開發者ID:RodMing,項目名稱:php-gcm,代碼行數:66,代碼來源:Sender.php

示例3: testSetsCanonicalRegistrationId

 public function testSetsCanonicalRegistrationId()
 {
     $result = new Result();
     $result->setCanonicalRegistrationId('canonical-registration-id');
     $this->assertEquals('canonical-registration-id', $result->getCanonicalRegistrationId());
 }
開發者ID:PiR-Alain,項目名稱:php-gcm,代碼行數:6,代碼來源:ResultTest.php


注:本文中的Result::setCanonicalRegistrationId方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。