当前位置: 首页>>代码示例>>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;未经允许,请勿转载。