2013-08-22 39 views
2

過去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給出錯誤。

謝謝大家。

+1

好了,看來,第一...即時發送簡單的格式,這就是爲什麼我得到虛假的我的套接字連接fread所有的時間(apns不發送簡單格式的錯誤)。我更新我的表單構建消息 – FEST

+0

我有完全相同的問題。你能告訴我,如果改變格式解決了你的問題?如果是這樣,那麼「簡單格式」是什麼意思?其他格式如何命名以及它們有什麼區別?一些源代碼將非常感謝! – Kup

回答

1

如果您收到一個錯誤(最有可能在你的情況,因爲你的發明設備令牌不會對蘋果的服務器存在你的應用程序),然後返回一個錯誤消息的連接關閉之前。如果消息成功,則沒有響應,但連接保持打開狀態。

這裏是你能怎麼處理這個問題:

  1. 發送郵件。
  2. 用超時(或非阻塞模式)讀取錯誤。如果您收到錯誤響應,則消息失敗。 (你需要一個新的連接。)
  3. 如果讀取超時(或非阻塞模式返回任何內容),消息可以成功。 (什麼都不做)
  4. 發送下一條消息。如果發送成功(仍然連接),最後的消息肯定是成功的。

保持跟蹤你的最後一次成功的消息。您可以隨時通過向自己的設備發送一條消息來追蹤一系列消息,以防止出現任何誤報。

你可能要檢查出的源代碼EasyAPNS,這是一個開源的PHP實現這一點。此外,你可以閱讀蘋果文檔中的細節:Provider Communication with Apple Push Notification Service

+0

這就是我遵循的理論......並且我檢查了EasyAPNS代碼,他們有fread(套接字連接),但對我來說,結果始終爲false/null。 :( – FEST

+0

@FEST你有沒有得到過這個呢?我處理同樣的問題,需要檢測狀態以查看哪些設備令牌是問題,因此我可以將它們從我的數據庫中刪除。 – tamak

5

你已經做了作爲$fp = stream_socket_client ......

您的連接和您要$errorResponse = @fread($apns, 6);這將永遠不會給你$ APNS沒有定義任何輸出。

相反,你應該這樣做:

$errorResponse = @fread($fp, 6); 

,你的代碼將正常工作