Apple のドキュメントには次のように書かれています。
「通知を送信し、APNs が通知の形式が正しくないか、またはその他の理由で理解できないことを検出した場合、切断する前にエラー応答パケットを返します。(エラーがない場合、APNs は何も返しません。) 図 5-3 は、次の形式を示しています。エラー応答パケット。」
これにより、APNS が何かを返送しない唯一の理由は、私が送信したものが正しい形式であった場合にあると私は信じています。ただし、彼らの応答を恐れようとすると、長さ0の文字列が返され、解凍するとnullになります。これは、何も書き戻されていないことを意味すると思います。
ストリームが開かれstream_socket_client()
、false を返したり、例外をスローしたりしませんでした。fwrite がそのストリームにも 154 バイトを正常に書き込んだことはわかっています。Apple からの応答がないのはなぜですか?
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ファイルを作成することで解決しました。