File indexing completed on 2025-03-02 05:29:36
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Mobile 0017 * @subpackage Zend_Mobile_Push 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Http_Client **/ 0024 // require_once 'Zend/Http/Client.php'; 0025 0026 /** Zend_Mobile_Push_Abstract **/ 0027 // require_once 'Zend/Mobile/Push/Abstract.php'; 0028 0029 /** Zend_Mobile_Push_Message_Gcm **/ 0030 // require_once 'Zend/Mobile/Push/Message/Gcm.php'; 0031 0032 /** Zend_Mobile_Push_Response_Gcm **/ 0033 // require_once 'Zend/Mobile/Push/Response/Gcm.php'; 0034 0035 /** 0036 * GCM Push 0037 * 0038 * @category Zend 0039 * @package Zend_Mobile 0040 * @subpackage Zend_Mobile_Push 0041 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0042 * @license http://framework.zend.com/license/new-bsd New BSD License 0043 * @version $Id$ 0044 */ 0045 class Zend_Mobile_Push_Gcm extends Zend_Mobile_Push_Abstract 0046 { 0047 0048 /** 0049 * @const string Server URI 0050 */ 0051 const SERVER_URI = 'https://android.googleapis.com/gcm/send'; 0052 0053 /** 0054 * Http Client 0055 * 0056 * @var Zend_Http_Client 0057 */ 0058 protected $_httpClient; 0059 0060 /** 0061 * API Key 0062 * 0063 * @var string 0064 */ 0065 protected $_apiKey; 0066 0067 /** 0068 * Get API Key 0069 * 0070 * @return string 0071 */ 0072 public function getApiKey() 0073 { 0074 return $this->_apiKey; 0075 } 0076 0077 /** 0078 * Set API Key 0079 * 0080 * @param string $key 0081 * @return Zend_Mobile_Push_Gcm 0082 * @throws Zend_Mobile_Push_Exception 0083 */ 0084 public function setApiKey($key) 0085 { 0086 if (!is_string($key) || empty($key)) { 0087 throw new Zend_Mobile_Push_Exception('The api key must be a string and not empty'); 0088 } 0089 $this->_apiKey = $key; 0090 return $this; 0091 } 0092 0093 /** 0094 * Get Http Client 0095 * 0096 * @return Zend_Http_Client 0097 */ 0098 public function getHttpClient() 0099 { 0100 if (!$this->_httpClient) { 0101 $this->_httpClient = new Zend_Http_Client(); 0102 $this->_httpClient->setConfig(array( 0103 'strictredirects' => true, 0104 )); 0105 } 0106 return $this->_httpClient; 0107 } 0108 0109 /** 0110 * Set Http Client 0111 * 0112 * @return Zend_Mobile_Push_Gcm 0113 */ 0114 public function setHttpClient(Zend_Http_Client $client) 0115 { 0116 $this->_httpClient = $client; 0117 return $this; 0118 } 0119 0120 /** 0121 * Send Message 0122 * 0123 * @param Zend_Mobile_Push_Message_Abstract $message 0124 * @throws Zend_Http_Client_Exception 0125 * @throws Zend_Mobile_Push_Exception 0126 * @throws Zend_Mobile_Push_Exception_InvalidAuthToken 0127 * @throws Zend_Mobile_Push_Exception_InvalidPayload 0128 * @throws Zend_Mobile_Push_Exception_ServerUnavailable 0129 * @return Zend_Mobile_Push_Response_Gcm 0130 */ 0131 public function send(Zend_Mobile_Push_Message_Abstract $message) 0132 { 0133 if (!$message->validate()) { 0134 throw new Zend_Mobile_Push_Exception('The message is not valid.'); 0135 } 0136 0137 $this->connect(); 0138 0139 $client = $this->getHttpClient(); 0140 $client->setUri(self::SERVER_URI); 0141 $client->setHeaders('Authorization', 'key=' . $this->getApiKey()); 0142 0143 $response = $client->setRawData($message->toJson(), 'application/json') 0144 ->request('POST'); 0145 $this->close(); 0146 0147 switch ($response->getStatus()) 0148 { 0149 case 500: 0150 // require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php'; 0151 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server encountered an internal error, try again'); 0152 break; 0153 case 503: 0154 // require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php'; 0155 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable, check Retry-After header'); 0156 break; 0157 case 401: 0158 // require_once 'Zend/Mobile/Push/Exception/InvalidAuthToken.php'; 0159 throw new Zend_Mobile_Push_Exception_InvalidAuthToken('There was an error authenticating the sender account'); 0160 break; 0161 case 400: 0162 // require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php'; 0163 throw new Zend_Mobile_Push_Exception_InvalidPayload('The request could not be parsed as JSON or contains invalid fields'); 0164 break; 0165 } 0166 return new Zend_Mobile_Push_Response_Gcm($response->getBody(), $message); 0167 } 0168 }