Exemple #1
0
 public function send(GCMMessage $message)
 {
     if (!$message->isValid()) {
         return $message->getErrors();
     }
     if (!$this->apiKey) {
         throw new IlegalApiKeyException("Api Key is empty");
     }
     if (count($message->getTo()) <= 0) {
         throw new NoRecipientsException("Add at least 1 recipient (GCM Registration ID)");
     }
     if (count($message->getTo()) > 1000) {
         throw new TooManyRecipientsException("Recipients maximum is 1000 GCM Registration IDs");
     }
     $data = $this->generateJSONMessage($message);
     if (!empty($data)) {
         if (strlen($data) > 4096) {
             throw new TooBigPayloadException("Data payload maximum is 4096 bytes");
         }
     }
     $headers = array('Content-Type: application/json', 'Authorization: key=' . $this->apiKey);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $this->url);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     $resultBody = curl_exec($ch);
     $resultHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     switch ($resultHttpCode) {
         case "200":
             break;
             // its ok
         // its ok
         case "400":
         case "401":
             throw new AuthenticationException("HTTP Authentication Error", $resultHttpCode);
         default:
             throw new HttpException("HTTP Error", $resultHttpCode);
     }
     $response = new GCMResponse($message, $resultBody);
     return $response;
 }