Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how to unpack Apple APNS feedback data

I've successfully managed to connect to Apple's feedback APNS server but I'm not sure how to unpack the binary data you get from fread(). Does anyone know how to do this? The documentation says the first 4 bytes are the timestamp, the next 2 are the token length and the rest are the device token.

How does this info get unpacked into readable characters after the call to fread?

like image 879
strange Avatar asked Mar 01 '23 04:03

strange


2 Answers

Once you have your binary stream, you can process it like this:

while ($data = fread($stream, 38)) {
  $feedback = unpack("N1timestamp/n1length/H*devtoken", $data);
  // Do something
}

$feedback will be an associative array containing elements "timestamp", "length" and "devtoken".

like image 93
Chris Newman Avatar answered Mar 12 '23 13:03

Chris Newman


Actually figured it out, this seems to be more reliable:

$arr = unpack("H*", $devconts); 
$rawhex = trim(implode("", $arr));

$feedbackTime = hexdec(substr($rawhex, 0, 8)); 
$feedbackDate = date('Y-m-d H:i', $feedbackTime); 
$feedbackLen = hexdec(substr($rawhex, 8, 4)); 
$feedbackDeviceToken = substr($rawhex, 12, 64);
like image 21
strange Avatar answered Mar 12 '23 12:03

strange