Mpns.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2015 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-2015 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 Zend_Http_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_Abstract $message
  75. * @throws Zend_Http_Client_Exception
  76. * @throws Zend_Mobile_Push_Exception
  77. * @throws Zend_Mobile_Push_Exception_DeviceQuotaExceeded
  78. * @throws Zend_Mobile_Push_Exception_InvalidPayload
  79. * @throws Zend_Mobile_Push_Exception_InvalidToken
  80. * @throws Zend_Mobile_Push_Exception_QuotaExceeded
  81. * @throws Zend_Mobile_Push_Exception_ServerUnavailable
  82. * @return boolean
  83. */
  84. public function send(Zend_Mobile_Push_Message_Abstract $message)
  85. {
  86. if (!$message->validate()) {
  87. throw new Zend_Mobile_Push_Exception('The message is not valid.');
  88. }
  89. $this->connect();
  90. $client = $this->getHttpClient();
  91. $client->setUri($message->getToken());
  92. $client->setHeaders(array(
  93. 'Context-Type' => 'text/xml',
  94. 'Accept' => 'application/*',
  95. 'X-NotificationClass' => $message->getDelay()
  96. ));
  97. if ($message->getId()) {
  98. $client->setHeaders('X-MessageID', $message->getId());
  99. }
  100. if ($message->getNotificationType() != Zend_Mobile_Push_Message_Mpns::TYPE_RAW) {
  101. $client->setHeaders('X-WindowsPhone-Target', $message->getNotificationType());
  102. }
  103. $client->setRawData($message->getXmlPayload(), 'text/xml');
  104. $response = $client->request('POST');
  105. $this->close();
  106. switch ($response->getStatus())
  107. {
  108. case 200:
  109. // check headers for response? need to test how this actually works to correctly handle different states.
  110. if ($response->getHeader('NotificationStatus') == 'QueueFull') {
  111. require_once 'Zend/Mobile/Push/Exception/DeviceQuotaExceeded.php';
  112. throw new Zend_Mobile_Push_Exception_DeviceQuotaExceeded('The devices push notification queue is full, use exponential backoff');
  113. }
  114. break;
  115. case 400:
  116. require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php';
  117. throw new Zend_Mobile_Push_Exception_InvalidPayload('The message xml was invalid');
  118. break;
  119. case 401:
  120. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  121. throw new Zend_Mobile_Push_Exception_InvalidToken('The device token is not valid or there is a mismatch between certificates');
  122. break;
  123. case 404:
  124. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  125. throw new Zend_Mobile_Push_Exception_InvalidToken('The device subscription is invalid, stop sending notifications to this device');
  126. break;
  127. case 405:
  128. throw new Zend_Mobile_Push_Exception('Invalid method, only POST is allowed'); // will never be hit unless overwritten
  129. break;
  130. case 406:
  131. require_once 'Zend/Mobile/Push/Exception/QuotaExceeded.php';
  132. throw new Zend_Mobile_Push_Exception_QuotaExceeded('The unauthenticated web service has reached the per-day throttling limit');
  133. break;
  134. case 412:
  135. require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
  136. throw new Zend_Mobile_Push_Exception_InvalidToken('The device is in an inactive state. You may retry once per hour');
  137. break;
  138. case 503:
  139. require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
  140. throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable.');
  141. break;
  142. default:
  143. break;
  144. }
  145. return true;
  146. }
  147. }