2012-11-02 123 views
0

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文件來解決。

+0

http://stackoverflow.com/questions/2293155/apple-push-notification-service-apns-notifications-not-arriving?rq=1 – KevinDTimm

+0

請發表您的代碼提交 – KevinDTimm

+0

針對您發佈的其他問題,我不是開發應用程序的開發人員,所以我不知道具體情況,但他說這是應用程序的臨時版本,我們沒有使用我認爲正確的沙箱。 –

回答

1

在這個頁面:

你應該定期與反饋網絡服務器連接,並獲取已多次報道過失敗的遞送嘗試這些設備的當前列表。然後,您應該停止向與這些應用程序相關的設備發送通知。有關更多信息,請參閱「反饋服務」。

反饋服務

訪問反饋服務,通過類似於用於發送推送通知的二進制接口進行。您通過feedback.push.apple.com,端口2196訪問生產反饋服務;您可以通過feedback.sandbox.push.apple.com,端口2196訪問沙盒反饋服務。與推送通知的二進制界面一樣,您必須使用TLS(或SSL)來建立安全通信通道。這些連接所需的SSL證書與供應發送通知的SSL證書相同。要建立可信賴的提供商身份,您應該在連接時使用對等身份驗證將此證書提供給APN。

+0

這不僅僅是用於刪除由於用戶刪除應用程序而過期的設備令牌的服務嗎?我試圖使用的設備令牌僅在2天前才提供給Web服務。 –

+0

據我所知,唯一的方法去了解爲什麼一些失敗的是反饋服務 – KevinDTimm

+0

好吧,非常感謝您的答覆。我要去嘗試一下,然後發佈結果。 –