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