過去3天我一直在努力使我的php代碼和apns工作。PHP Apple APNS閱讀IMMEDIATE通知Status_code
到目前爲止,我能夠交付通知(我只有一臺設備,所以我不知道這是工作的多個設備)。
當我嘗試測試發送一個通知爲2設備,其中第一個是設備令牌無效(由我發明),第二個是我的設備令牌,我無法將通知傳遞到我的設備...從什麼我讀過,當通知不可讀或錯誤時,apns會關閉連接併發送一個6字節長的錯誤,其中第二個字節(status_code)是錯誤類型。如果沒有錯誤發生,我仍然無法理解,如果apns發送status_code 0(沒有遇到錯誤)或者它根本不發送任何東西。
到目前爲止,我沒能不能找到這個STATUS_CODE,儘管下面的互聯網上找到一些代碼。
CODE
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '.\certif\ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Create the payload body
//file_put_contents('logs/debug_not.log', 'body'. "\n", FILE_APPEND);
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
//file_put_contents('logs/debug_not.log', 'json body'. "\n", FILE_APPEND);
$payload = json_encode($body);
$open = true;
foreach ($devices as $device) {
//file_put_contents('logs/debug_not.log', 'inicio ciclo'. "\n", FILE_APPEND);
if($open == true){
//file_put_contents('logs/debug_not.log', 'abrir ligacao'. "\n", FILE_APPEND);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
//file_put_contents('logs/debug_not.log', 'erro abrir ligacao'. "\n", FILE_APPEND);
throw new \Exception;
}
}
// Build the binary notification
//file_put_contents('logs/debug_not.log', 'criar payload'. "\n", FILE_APPEND);
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $device['token'])) . pack('n', strlen($payload)) . $payload;
// Send it to the server
//file_put_contents('logs/debug_not.log', 'enviar payload'. "\n", FILE_APPEND);
$result = fwrite($fp, $msg, strlen($msg));
stream_set_blocking ($fp, 0);
$errorResponse = @fread($apns, 6);
//if i var_dump($errorResponse) it gives me null/false all the time, no matter what
//this is my workaround for invalid device tokens-- its not working or at least is not send the valid tokens
if(!$result || !$fp)
{
//file_put_contents('logs/debug_not.log', 'erro na not '. chr($data["status_code"]). "\n", FILE_APPEND);
fclose($fp);
$open = true;
} else{
//file_put_contents('logs/debug_not.log', 'suc na not'. "\n", FILE_APPEND);
$open = false;
}
//file_put_contents('logs/debug_not.log', 'fim ciclo'. "\n", FILE_APPEND);
}
// Close the connection to the server
if($fp){
//file_put_contents('logs/debug_not.log', 'fechar connection'. "\n", FILE_APPEND);
fclose($fp);
}
我連接上FREAD給空/假的時候,甚至當我收到我的通知,或者我與一個錯誤的令牌嘗試。
我的邏輯錯誤後連接,似乎不工作。
可以請別人幫我嗎?我不打算使用第三類或代碼,希望有一個簡單的基本工作的設備令牌數組,即使有一些apns給出錯誤。
謝謝大家。
好了,看來,第一...即時發送簡單的格式,這就是爲什麼我得到虛假的我的套接字連接fread所有的時間(apns不發送簡單格式的錯誤)。我更新我的表單構建消息 – FEST
我有完全相同的問題。你能告訴我,如果改變格式解決了你的問題?如果是這樣,那麼「簡單格式」是什麼意思?其他格式如何命名以及它們有什麼區別?一些源代碼將非常感謝! – Kup