Gcm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_Gcm **/
  27. require_once 'Zend/Mobile/Push/Message/Gcm.php';
  28. /** Zend_Mobile_Push_Response_Gcm **/
  29. require_once 'Zend/Mobile/Push/Response/Gcm.php';
  30. /**
  31. * GCM Push
  32. *
  33. * @category Zend
  34. * @package Zend_Mobile
  35. * @subpackage Zend_Mobile_Push
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @version $Id$
  39. */
  40. class Zend_Mobile_Push_Gcm extends Zend_Mobile_Push_Abstract
  41. {
  42. /**
  43. * @const string Server URI
  44. */
  45. const SERVER_URI = 'https://android.googleapis.com/gcm/send';
  46. /**
  47. * Http Client
  48. *
  49. * @var Client
  50. */
  51. protected $_httpClient;
  52. /**
  53. * API Key
  54. *
  55. * @var string
  56. */
  57. protected $_apiKey;
  58. /**
  59. * Get API Key
  60. *
  61. * @return string
  62. */
  63. public function getApiKey()
  64. {
  65. return $this->_apiKey;
  66. }
  67. /**
  68. * Set API Key
  69. *
  70. * @param string $key
  71. * @return Zend_Mobile_Push_Gcm
  72. * @throws Zend_Mobile_Push_Exception
  73. */
  74. public function setApiKey($key)
  75. {
  76. if (!is_string($key) || empty($key)) {
  77. throw new Zend_Mobile_Push_Exception('The api key must be a string and not empty');
  78. }
  79. $this->_apiKey = $key;
  80. return $this;
  81. }
  82. /**
  83. * Get Http Client
  84. *
  85. * @return Zend_Http_Client
  86. */
  87. public function getHttpClient()
  88. {
  89. if (!$this->_httpClient) {
  90. $this->_httpClient = new Zend_Http_Client();
  91. $this->_httpClient->setConfig(array(
  92. 'strictredirects' => true,
  93. ));
  94. }
  95. return $this->_httpClient;
  96. }
  97. /**
  98. * Set Http Client
  99. *
  100. * @return Zend_Mobile_Push_Gcm
  101. */
  102. public function setHttpClient(Zend_Http_Client $client)
  103. {
  104. $this->_httpClient = $client;
  105. return $this;
  106. }
  107. /**
  108. * Send Message
  109. *
  110. * @param Zend_Mobile_Push_Message_Gcm $message
  111. * @return boolean
  112. * @throws Zend_Mobile_Push_Exception
  113. */
  114. public function send(Zend_Mobile_Push_Message_Abstract $message)
  115. {
  116. if (!$message->validate()) {
  117. throw new Zend_Mobile_Push_Exception('The message is not valid.');
  118. }
  119. $this->connect();
  120. $client = $this->getHttpClient();
  121. $client->setUri(self::SERVER_URI);
  122. $client->setHeaders('Authorization', 'key=' . $this->getApiKey());
  123. $json = array('registration_ids' => $message->getToken());
  124. if ($data = $message->getData()) {
  125. $json['data'] = $data;
  126. }
  127. if ($id = $message->getId()) {
  128. $json['id'] = $id;
  129. }
  130. $response = $client->setRawData($message->toJson(), 'application/json')
  131. ->request('POST');
  132. $this->close();
  133. switch ($response->getStatus())
  134. {
  135. case 500:
  136. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  137. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server encountered an internal error, try again');
  138. break;
  139. case 503:
  140. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  141. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable, check Retry-After header');
  142. break;
  143. case 401:
  144. require_once 'Zend/Mobile/Push/Exception/InvalidAuthToken.php';
  145. throw new Zend_Mobile_Push_Exception_InvalidAuthToken('There was an error authenticating the sender account');
  146. break;
  147. case 400:
  148. require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php';
  149. throw new Zend_Mobile_Push_Exception_InvalidPayload('The request could not be parsed as JSON or contains invalid fields');
  150. break;
  151. }
  152. return new Zend_Mobile_Push_Response_Gcm($response->getBody(), $message);
  153. }
  154. }