apns-php
 All Data Structures Files Functions Variables Groups Pages
Feedback.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file
4  * ApnsPHP_Feedback class definition.
5  *
6  * LICENSE
7  *
8  * This source file is subject to the new BSD license that is bundled
9  * with this package in the file LICENSE.txt.
10  * It is also available through the world-wide-web at this URL:
11  * http://code.google.com/p/apns-php/wiki/License
12  * If you did not receive a copy of the license and are unable to
13  * obtain it through the world-wide-web, please send an email
14  * to aldo.armiento@gmail.com so we can send you a copy immediately.
15  *
16  * @author (C) 2010 Aldo Armiento (aldo.armiento@gmail.com)
17  * @version $Id$
18  */
19 
20 /**
21  * @defgroup ApnsPHP_Feedback Feedback
22  * @ingroup ApplePushNotificationService
23  */
24 
25 /**
26  * The Feedback Service client.
27  *
28  * Apple Push Notification Service includes a feedback service that APNs continually
29  * updates with a per-application list of devices for which there were failed-delivery
30  * attempts. Providers should periodically query the feedback service to get the
31  * list of device tokens for their applications, each of which is identified by
32  * its topic. Then, after verifying that the application hasn’t recently been re-registered
33  * on the identified devices, a provider should stop sending notifications to these
34  * devices.
35  *
36  * @ingroup ApnsPHP_Feedback
37  * @see http://tinyurl.com/ApplePushNotificationFeedback
38  */
40 {
41  const TIME_BINARY_SIZE = 4; /**< @type integer Timestamp binary size in bytes. */
42  const TOKEN_LENGTH_BINARY_SIZE = 2; /**< @type integer Token length binary size in bytes. */
43 
44  protected $_aServiceURLs = array(
45  'tls://feedback.push.apple.com:2196', // Production environment
46  'tls://feedback.sandbox.push.apple.com:2196' // Sandbox environment
47  ); /**< @type array Feedback URLs environments. */
48 
49  protected $_aFeedback; /**< @type array Feedback container. */
50 
51  /**
52  * Receives feedback tuples from Apple Push Notification Service feedback.
53  *
54  * Every tuple (array) contains:
55  * @li @c timestamp indicating when the APNs determined that the application
56  * no longer exists on the device. This value represents the seconds since
57  * 1970, anchored to UTC. You should use the timestamp to determine if the
58  * application on the device re-registered with your service since the moment
59  * the device token was recorded on the feedback service. If it hasn’t,
60  * you should cease sending push notifications to the device.
61  * @li @c tokenLength The length of the device token (usually 32 bytes).
62  * @li @c deviceToken The device token.
63  *
64  * @return @type array Array of feedback tuples (array).
65  */
66  public function receive()
67  {
68  $nFeedbackTupleLen = self::TIME_BINARY_SIZE + self::TOKEN_LENGTH_BINARY_SIZE + self::DEVICE_BINARY_SIZE;
69 
70  $this->_aFeedback = array();
71  $sBuffer = '';
72  while (!feof($this->_hSocket)) {
73  $this->_log('INFO: Reading...');
74  $sBuffer .= $sCurrBuffer = fread($this->_hSocket, 8192);
75  $nCurrBufferLen = strlen($sCurrBuffer);
76  if ($nCurrBufferLen > 0) {
77  $this->_log("INFO: {$nCurrBufferLen} bytes read.");
78  }
79  unset($sCurrBuffer, $nCurrBufferLen);
80 
81  $nBufferLen = strlen($sBuffer);
82  if ($nBufferLen >= $nFeedbackTupleLen) {
83  $nFeedbackTuples = floor($nBufferLen / $nFeedbackTupleLen);
84  for ($i = 0; $i < $nFeedbackTuples; $i++) {
85  $sFeedbackTuple = substr($sBuffer, 0, $nFeedbackTupleLen);
86  $sBuffer = substr($sBuffer, $nFeedbackTupleLen);
87  $this->_aFeedback[] = $aFeedback = $this->_parseBinaryTuple($sFeedbackTuple);
88  $this->_log(sprintf("INFO: New feedback tuple: timestamp=%d (%s), tokenLength=%d, deviceToken=%s.",
89  $aFeedback['timestamp'], date('Y-m-d H:i:s', $aFeedback['timestamp']),
90  $aFeedback['tokenLength'], $aFeedback['deviceToken']
91  ));
92  unset($aFeedback);
93  }
94  }
95 
96  $read = array($this->_hSocket);
97  $null = NULL;
98  $nChangedStreams = stream_select($read, $null, $null, 0, $this->_nSocketSelectTimeout);
99  if ($nChangedStreams === false) {
100  $this->_log('WARNING: Unable to wait for a stream availability.');
101  break;
102  }
103  }
104  return $this->_aFeedback;
105  }
106 
107  /**
108  * Parses binary tuples.
109  *
110  * @param $sBinaryTuple @type string A binary tuple to parse.
111  * @return @type array Array with timestamp, tokenLength and deviceToken keys.
112  */
113  protected function _parseBinaryTuple($sBinaryTuple)
114  {
115  return unpack('Ntimestamp/ntokenLength/H*deviceToken', $sBinaryTuple);
116  }
117 }
const TIME_BINARY_SIZE
integer Timestamp binary size in bytes.
Definition: Feedback.php:41
array _parseBinaryTuple(string $sBinaryTuple)
Parses binary tuples.
Definition: Feedback.php:113
const TOKEN_LENGTH_BINARY_SIZE
integer Token length binary size in bytes.
Definition: Feedback.php:42
Abstract class: this is the superclass for all Apple Push Notification Service classes.
Definition: Abstract.php:40
array receive()
Receives feedback tuples from Apple Push Notification Service feedback.
Definition: Feedback.php:66
void _log(string $sMessage)
Logs a message through the Logger.
Definition: Abstract.php:414
The Feedback Service client.
Definition: Feedback.php:39
$_aServiceURLs
array Feedback URLs environments.
Definition: Feedback.php:44
array $_aFeedback
Feedback container.
Definition: Feedback.php:49