|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Mobile |
|
17 * @subpackage Zend_Mobile_Push |
|
18 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id$ |
|
21 */ |
|
22 |
|
23 /** Zend_Http_Client **/ |
|
24 require_once 'Zend/Http/Client.php'; |
|
25 |
|
26 /** Zend_Mobile_Push_Abstract **/ |
|
27 require_once 'Zend/Mobile/Push/Abstract.php'; |
|
28 |
|
29 /** Zend_Mobile_Push_Message_Gcm **/ |
|
30 require_once 'Zend/Mobile/Push/Message/Gcm.php'; |
|
31 |
|
32 /** Zend_Mobile_Push_Response_Gcm **/ |
|
33 require_once 'Zend/Mobile/Push/Response/Gcm.php'; |
|
34 |
|
35 /** |
|
36 * GCM Push |
|
37 * |
|
38 * @category Zend |
|
39 * @package Zend_Mobile |
|
40 * @subpackage Zend_Mobile_Push |
|
41 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
|
42 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
43 * @version $Id$ |
|
44 */ |
|
45 class Zend_Mobile_Push_Gcm extends Zend_Mobile_Push_Abstract |
|
46 { |
|
47 |
|
48 /** |
|
49 * @const string Server URI |
|
50 */ |
|
51 const SERVER_URI = 'https://android.googleapis.com/gcm/send'; |
|
52 |
|
53 /** |
|
54 * Http Client |
|
55 * |
|
56 * @var Client |
|
57 */ |
|
58 protected $_httpClient; |
|
59 |
|
60 /** |
|
61 * API Key |
|
62 * |
|
63 * @var string |
|
64 */ |
|
65 protected $_apiKey; |
|
66 |
|
67 /** |
|
68 * Get API Key |
|
69 * |
|
70 * @return string |
|
71 */ |
|
72 public function getApiKey() |
|
73 { |
|
74 return $this->_apiKey; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Set API Key |
|
79 * |
|
80 * @param string $key |
|
81 * @return Zend_Mobile_Push_Gcm |
|
82 * @throws Zend_Mobile_Push_Exception |
|
83 */ |
|
84 public function setApiKey($key) |
|
85 { |
|
86 if (!is_string($key) || empty($key)) { |
|
87 throw new Zend_Mobile_Push_Exception('The api key must be a string and not empty'); |
|
88 } |
|
89 $this->_apiKey = $key; |
|
90 return $this; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Get Http Client |
|
95 * |
|
96 * @return Zend_Http_Client |
|
97 */ |
|
98 public function getHttpClient() |
|
99 { |
|
100 if (!$this->_httpClient) { |
|
101 $this->_httpClient = new Zend_Http_Client(); |
|
102 $this->_httpClient->setConfig(array( |
|
103 'strictredirects' => true, |
|
104 )); |
|
105 } |
|
106 return $this->_httpClient; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Set Http Client |
|
111 * |
|
112 * @return Zend_Mobile_Push_Gcm |
|
113 */ |
|
114 public function setHttpClient(Zend_Http_Client $client) |
|
115 { |
|
116 $this->_httpClient = $client; |
|
117 return $this; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Send Message |
|
122 * |
|
123 * @param Zend_Mobile_Push_Message_Gcm $message |
|
124 * @return Zend_Mobile_Push_Response_Gcm |
|
125 * @throws Zend_Mobile_Push_Exception |
|
126 */ |
|
127 public function send(Zend_Mobile_Push_Message_Abstract $message) |
|
128 { |
|
129 if (!$message->validate()) { |
|
130 throw new Zend_Mobile_Push_Exception('The message is not valid.'); |
|
131 } |
|
132 |
|
133 $this->connect(); |
|
134 |
|
135 $client = $this->getHttpClient(); |
|
136 $client->setUri(self::SERVER_URI); |
|
137 $client->setHeaders('Authorization', 'key=' . $this->getApiKey()); |
|
138 |
|
139 $json = array('registration_ids' => $message->getToken()); |
|
140 if ($data = $message->getData()) { |
|
141 $json['data'] = $data; |
|
142 } |
|
143 if ($id = $message->getId()) { |
|
144 $json['id'] = $id; |
|
145 } |
|
146 |
|
147 $response = $client->setRawData($message->toJson(), 'application/json') |
|
148 ->request('POST'); |
|
149 $this->close(); |
|
150 |
|
151 switch ($response->getStatus()) |
|
152 { |
|
153 case 500: |
|
154 require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php'; |
|
155 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server encountered an internal error, try again'); |
|
156 break; |
|
157 case 503: |
|
158 require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php'; |
|
159 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable, check Retry-After header'); |
|
160 break; |
|
161 case 401: |
|
162 require_once 'Zend/Mobile/Push/Exception/InvalidAuthToken.php'; |
|
163 throw new Zend_Mobile_Push_Exception_InvalidAuthToken('There was an error authenticating the sender account'); |
|
164 break; |
|
165 case 400: |
|
166 require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php'; |
|
167 throw new Zend_Mobile_Push_Exception_InvalidPayload('The request could not be parsed as JSON or contains invalid fields'); |
|
168 break; |
|
169 } |
|
170 return new Zend_Mobile_Push_Response_Gcm($response->getBody(), $message); |
|
171 } |
|
172 } |