Mpns.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_Mpns **/
  27. require_once 'Zend/Mobile/Push/Message/Mpns.php';
  28. /**
  29. * Mpns 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_Mpns extends Zend_Mobile_Push_Abstract
  39. {
  40. /**
  41. * Http Client
  42. *
  43. * @var Client
  44. */
  45. protected $_httpClient;
  46. /**
  47. * Get Http Client
  48. *
  49. * @return Zend_Http_Client
  50. */
  51. public function getHttpClient()
  52. {
  53. if (!$this->_httpClient) {
  54. $this->_httpClient = new Zend_Http_Client();
  55. $this->_httpClient->setConfig(array(
  56. 'strictredirects' => true,
  57. ));
  58. }
  59. return $this->_httpClient;
  60. }
  61. /**
  62. * Set Http Client
  63. *
  64. * @return Zend_Mobile_Push_Mpns
  65. */
  66. public function setHttpClient(Zend_Http_Client $client)
  67. {
  68. $this->_httpClient = $client;
  69. return $this;
  70. }
  71. /**
  72. * Send Message
  73. *
  74. * @param Zend_Mobile_Push_Message_Mpns $message
  75. * @return boolean
  76. * @throws Zend_Mobile_Push_Exception
  77. */
  78. public function send(Zend_Mobile_Push_Message_Abstract $message)
  79. {
  80. if (!$message->validate()) {
  81. throw new Zend_Mobile_Push_Exception('The message is not valid.');
  82. }
  83. $this->connect();
  84. $client = $this->getHttpClient();
  85. $client->setUri($message->getToken());
  86. $client->setHeaders(array(
  87. 'Context-Type' => 'text/xml',
  88. 'Accept' => 'application/*',
  89. 'X-NotificationClass' => $message->getDelay()
  90. ));
  91. if ($message->getId()) {
  92. $client->setHeaders('X-MessageID', $message->getId());
  93. }
  94. if ($message->getNotificationType() != Zend_Mobile_Push_Message_Mpns::TYPE_RAW) {
  95. $client->setHeaders('X-WindowsPhone-Target', $message->getNotificationType());
  96. }
  97. $client->setRawData($message->getXmlPayload(), 'text/xml');
  98. $response = $client->request('POST');
  99. $this->close();
  100. switch ($response->getStatus())
  101. {
  102. case 200:
  103. // check headers for response? need to test how this actually works to correctly handle different states.
  104. if ($response->getHeader('NotificationStatus') == 'QueueFull') {
  105. require_once 'Zend/Mobile/Push/Exception/DeviceQuotaExceeded.php';
  106. throw new Zend_Mobile_Push_Exception_DeviceQuotaExceeded('The devices push notification queue is full, use exponential backoff');
  107. }
  108. break;
  109. case 400:
  110. require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php';
  111. throw new Zend_Mobile_Push_Exception_InvalidPayload('The message xml was invalid');
  112. break;
  113. case 401:
  114. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  115. throw new Zend_Mobile_Push_Exception_InvalidToken('The device token is not valid or there is a mismatch between certificates');
  116. break;
  117. case 404:
  118. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  119. throw new Zend_Mobile_Push_Exception_InvalidToken('The device subscription is invalid, stop sending notifications to this device');
  120. break;
  121. case 405:
  122. throw new Zend_Mobile_Push_Exception('Invalid method, only POST is allowed'); // will never be hit unless overwritten
  123. break;
  124. case 406:
  125. require_once 'Zend/Mobile/Push/Exception/QuotaExceeded.php';
  126. throw new Zend_Mobile_Push_Exception_QuotaExceeded('The unauthenticated web service has reached the per-day throttling limit');
  127. break;
  128. case 412:
  129. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  130. throw new Zend_Mobile_Push_Exception_InvalidToken('The device is in an inactive state. You may retry once per hour');
  131. break;
  132. case 503:
  133. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  134. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable.');
  135. break;
  136. default:
  137. break;
  138. }
  139. return true;
  140. }
  141. }