Apple's docs說:APNS不發送響應,也不發送推送通知
「如果你發送通知和APN的認定通知畸形或不能理解,它斷開之前返回錯誤響應包(如果。沒有錯誤,APN不返回任何東西。)圖5-3描述了錯誤響應數據包的格式。「
這使我相信,APNS不會發回東西的唯一原因是,如果我發送它們的格式是正確的。然而,當我嘗試fread他們的迴應時,我得到一個0長度的字符串,當解包變爲null時,我認爲這意味着什麼都沒有寫回給我。
我的流被打開了stream_socket_client()
並沒有返回false或拋出異常。我知道我的fwrite也成功地寫了154字節。爲什麼蘋果沒有迴應?
這裏是用於連接到APNS的代碼:
function openConnection() {
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->APNS_CERT);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->Password);
$apns = stream_socket_client('ssl://' . $this -> APNS_HOST . ':' . $this -> APNS_PORT, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
return $apns;
}
然後,在調用OpenConnection之後的下一個方法:
//open the connection to use with apple.
$apns = $this->openConnection();
$alert = $message['alert'];
$badge = $message['badge'];
$deviceToken = $message['deviceToken'];
$payload['aps'] = array(
'alert' => $message['alert'],
'badge' => $message['badge'],
'sound' => $message['sound']
);
if ($message['extraPayload'] != null) {
$payload['acme'] = $message['extraPayload'];
}
$encodedString = json_encode($payload);
//create message
$apnsMessage = chr(1) . pack("N", $message['identifier']) . pack("N", $message['expire']) . pack("n", 32) . pack('H*', str_replace(' ', '', $message['deviceToken'])) . pack("n",strlen($encodedString)) . $encodedString;
$write = fwrite($apns, $apnsMessage);
echo $write;
//the echo was just to see if it wrote.
if (!$apns) {
socket_close($apns);
fclose($apns);
echo "connection to APNS was lost.";
}
//look for changes. $null=null because some bug doesn't just let you pass null.
$null = null;
$changedStreams = stream_select($streamArray, $null, $null, 0, 1000000);
//check if it is actually false
if ($changedStreams === false) {
//close stream when done.
socket_close($apns);
fclose($apns);
echo "No response from APNs";
} elseif ($changedStreams > 0) {
//then check if what they sent back is an error and grab the error packet
$responseBinary = fread($apns, 6);
var_dump($responseBinary);
//check that it's the right thing
if ($responseBinary != false || strlen($responseBinary) == 6) {
//convert it from it's binary stream state and print.
$response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
var_dump($response);
//close stream when done.
socket_close($apns);
fclose($apns);
}
} else {
echo "Apple failed to respond, message was not sent.";
}
的var_dump
在端部是NULL
。
編輯:
原來,這是一個憑證衝突的錯誤。它通過創建一個新的pem文件來解決。
http://stackoverflow.com/questions/2293155/apple-push-notification-service-apns-notifications-not-arriving?rq=1 – KevinDTimm
請發表您的代碼提交 – KevinDTimm
針對您發佈的其他問題,我不是開發應用程序的開發人員,所以我不知道具體情況,但他說這是應用程序的臨時版本,我們沒有使用我認爲正確的沙箱。 –