C2dm.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /** Zend_Http_Client **/
  23. require_once 'Zend/Http/Client.php';
  24. /** Zend_Mobile_Push_Abstract **/
  25. require_once 'Zend/Mobile/Push/Abstract.php';
  26. /** Zend_Mobile_Push_Message_C2dm **/
  27. require_once 'Zend/Mobile/Push/Message/C2dm.php';
  28. /**
  29. * C2DM Push
  30. *
  31. * @category Zend
  32. * @package Zend_Mobile
  33. * @subpackage Zend_Mobile_Push
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @version $Id$
  37. */
  38. class Zend_Mobile_Push_C2dm extends Zend_Mobile_Push_Abstract
  39. {
  40. /**
  41. * @const string Server URI
  42. */
  43. const SERVER_URI = 'https://android.apis.google.com/c2dm/send';
  44. /**
  45. * @const string ClientLogin auth service name
  46. */
  47. const AUTH_SERVICE_NAME = 'ac2dm';
  48. /**
  49. * Http Client
  50. *
  51. * @var Client
  52. */
  53. protected $_httpClient;
  54. /**
  55. * Login Token
  56. *
  57. * @var string
  58. */
  59. protected $_loginToken;
  60. /**
  61. * Get Login Token
  62. *
  63. * @return string
  64. */
  65. public function getLoginToken()
  66. {
  67. return $this->_loginToken;
  68. }
  69. /**
  70. * Set Login Token
  71. *
  72. * @param string $token
  73. * @return Zend_Mobile_Push_C2dm
  74. * @throws Zend_Mobile_Push_Exception
  75. */
  76. public function setLoginToken($token)
  77. {
  78. if (!is_string($token) || empty($token)) {
  79. throw new Zend_Mobile_Push_Exception('The login token must be a string and not empty');
  80. }
  81. $this->_loginToken = $token;
  82. return $this;
  83. }
  84. /**
  85. * Get Http Client
  86. *
  87. * @return Zend_Http_Client
  88. */
  89. public function getHttpClient()
  90. {
  91. if (!$this->_httpClient) {
  92. $this->_httpClient = new Zend_Http_Client();
  93. $this->_httpClient->setConfig(array(
  94. 'strictredirects' => true,
  95. ));
  96. }
  97. return $this->_httpClient;
  98. }
  99. /**
  100. * Set Http Client
  101. *
  102. * @return Zend_Mobile_Push_C2dm
  103. */
  104. public function setHttpClient(Zend_Http_Client $client)
  105. {
  106. $this->_httpClient = $client;
  107. return $this;
  108. }
  109. /**
  110. * Send Message
  111. *
  112. * @param Zend_Mobile_Push_Message_C2dm $message
  113. * @return boolean
  114. * @throws Zend_Mobile_Push_Exception
  115. */
  116. public function send(Zend_Mobile_Push_Message_Abstract $message)
  117. {
  118. if (!$message->validate()) {
  119. throw new Zend_Mobile_Push_Exception('The message is not valid.');
  120. }
  121. $this->connect();
  122. $client = $this->getHttpClient();
  123. $client->setUri(self::SERVER_URI);
  124. $client->setHeaders('Authorization', 'GoogleLogin auth=' . $this->getLoginToken());
  125. $client->setParameterPost('delay_while_idle', (int) $message->getDelayWhileIdle());
  126. $client->setParameterPost('registration_id', $message->getToken());
  127. $client->setParameterPost('collapse_key', $message->getId());
  128. foreach ($message->getData() as $k => $v) {
  129. $client->setParameterPost('data.' . $k, $v);
  130. }
  131. $response = $client->request('POST');
  132. $this->close();
  133. switch ($response->getStatus())
  134. {
  135. case 500:
  136. case 503:
  137. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  138. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable, check Retry-After header');
  139. break;
  140. case 401:
  141. require_once 'Zend/Mobile/Push/Exception/InvalidAuthToken.php';
  142. throw new Zend_Mobile_Push_Exception_InvalidAuthToken('The auth token is invalid');
  143. break;
  144. default:
  145. $body = $response->getBody();
  146. $body = preg_split('/=/', $body);
  147. if (!isset($body[0]) || !isset($body[1])) {
  148. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  149. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server gave us an invalid response, try again later');
  150. }
  151. if (strtolower($body[0]) == 'error') {
  152. $err = strtolower($body[1]);
  153. switch ($err) {
  154. case 'quotaexceeded':
  155. require_once 'Zend/Mobile/Push/Exception/QuotaExceeded.php';
  156. throw new Zend_Mobile_Push_Exception_QuotaExceeded('Too many messages sent by the sender. Retry after a while.');
  157. break;
  158. case 'devicequotaexceeded':
  159. require_once 'Zend/Mobile/Push/Exception/DeviceQuotaExceeded.php';
  160. throw new Zend_Mobile_Push_Exception_DeviceQuotaExceeded('Too many messages sent by the sender to a specific device. Retry after a while.');
  161. break;
  162. case 'missingregistration':
  163. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  164. throw new Zend_Mobile_Push_Exception_InvalidToken('Missing token. The message must always have a token.');
  165. break;
  166. case 'invalidregistration':
  167. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  168. throw new Zend_Mobile_Push_Exception_InvalidToken('Bad token. Remove this token from being sent to again.');
  169. break;
  170. case 'mismatchsenderid':
  171. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  172. throw new Zend_Mobile_Push_Exception_InvalidToken('Bad token. This token is not registered with the current login');
  173. break;
  174. case 'notregistered':
  175. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  176. throw new Zend_Mobile_Push_Exception_InvalidToken('Bad token. This token is not registered.');
  177. break;
  178. case 'messagetoobig':
  179. require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php';
  180. throw new Zend_Mobile_Push_Exception_InvalidPayload('The message is too big; reduce the size of the message.');
  181. break;
  182. case 'missingcollapsekey':
  183. require_once 'Zend/Mobile/Push/Exception/InvalidTopic.php';
  184. throw new Zend_Mobile_Push_Exception_InvalidTopic('The message id must be set; include one in the message.');
  185. break;
  186. default:
  187. $err = strip_tags($body[1]);
  188. throw new Zend_Mobile_Push_Exception(sprintf('An unknown error occurred: %s', $err));
  189. }
  190. }
  191. break;
  192. }
  193. return true;
  194. }
  195. }