Ejemplo n.º 1
0
 /**
  * Send message to GCM
  *
  * @param Message $message The Message to send.
  * @param string  $serverApiKey Server API Key.
  * @param string  $gcmUrl GCM URL.
  * @param integer $nextDelay Next exponential back off delay.
  *
  * @return Response
  * @throws PhpGcmQueueException When request is malformed
  * @throws PhpGcmQueueException When authentication fails
  * @throws PhpGcmQueueException When an unknown error occurs
  */
 public static function send(Message $message, $serverApiKey, $gcmUrl, $nextDelay = 1)
 {
     $curl = self::initCurl();
     self::configCurl($curl, $serverApiKey);
     self::postCurl($curl, $gcmUrl, $message->toJson());
     self::closeCurl($curl);
     if ($curl->error) {
         switch ($curl->http_status_code) {
             case 400:
                 throw new PhpGcmQueueException('GCM\\Sender->send - Malformed Request: ' . $curl->raw_response, PhpGcmQueueException::MALFORMED_REQUEST);
                 break;
             case 401:
                 throw new PhpGcmQueueException('GCM\\Sender->send - Authentication Error', PhpGcmQueueException::AUTHENTICATION_ERROR);
                 break;
             default:
                 $retry = $curl->response_headers['retry-after'];
                 if ($retry) {
                     if ((int) $retry) {
                         //in seconds: 120
                         $retry = \DateTime::createFromFormat('U', strtotime('now +' . (int) $retry . ' seconds'));
                     } else {
                         //absolute: Fri, 31 Dec 1999 23:59:59 GMT
                         $retry = \DateTime::createFromFormat('U', strtotime($retry));
                     }
                     return $retry;
                 } else {
                     //Timeout
                     if ($curl->http_status_code >= 501 && $curl->http_status_code <= 599) {
                         if ($nextDelay < self::MAX_RETRY_TIME) {
                             return $nextDelay;
                         }
                     }
                     throw new PhpGcmQueueException('GCM\\Sender->send - Unknown Error: ' . $curl->raw_response, PhpGcmQueueException::UNKNOWN_ERROR);
                 }
                 break;
         }
     }
     return new Response($message, $curl->response);
 }
Ejemplo n.º 2
0
            $messageArray['time_to_live'] = $timeToLive;
        }
        if (!empty($restrictedPackageName)) {
            $messageArray['restricted_package_name'] = $restrictedPackageName;
        }
        if (isset($dryRun)) {
            $messageArray['dry_run'] = (bool) $dryRun;
        }
        //Payload
        if (!empty($data)) {
            $messageArray['data'] = json_decode($data, true);
        }
        if (!empty($notification)) {
            $messageArray['notification'] = json_decode($notification, true);
        }
        $message = \PhpGcmQueue\Message::fromArray($messageArray);
        $response = \PhpGcmQueue\Sender::send($message, $serverApiKey, $gcmUrl);
    } else {
        throw new Exception('Missing required fields');
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <title>PHP GCM Queue Example</title>
    <meta name="description" content="PHP GCM Queue Example">
    <meta name="author" content="stevetauber">
    <meta name="viewport" content="width=device-width, initial-scale=1">