2014-11-04 75 views
0

如果是重複問題,但我無法找到我正在尋找的解決方案,我很抱歉。以下是我的PHP服務代碼在iPhone上發送通知:無法通過PHP服務在iPhone上接收蘋果推送通知

<?php 
    $data = array(); 
    // Put your device token here (without spaces): 
    $deviceToken = 'eb9ea0d12eb9a0bae159c7e54fa59baee22329df'; 
    // Put your private key's passphrase here: 
    $passphrase = 'dell'; 
    $message = 'Push notification service'; 
    //////////////////////////////////////////////////////////////////////////////// 
    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'application/controllers/ck.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
    // 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) 
     // exit("Failed to connect: $err $errstr" . PHP_EOL); 

    $data['response'] = 'Connected to APNS' . PHP_EOL; 

    // Create the payload body 
    $body['aps'] = array(
     'alert' => $message, 
     'sound' => 'default' 
     ); 

    // Encode the payload as JSON 
    $payload = json_encode($body); 

    // Build the binary notification 
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .$payload; 

    // Send it to the server 
    $result = fwrite($fp, $msg, strlen($msg)); 

    if (!$result) 
     $data['response'] = 'Message not delivered' . PHP_EOL; 
    else 
     $data['response'] = 'Message successfully delivered' . PHP_EOL; 

    // Close the connection to the server 
    fclose($fp); 
    $response->skills = $data; 
    $this->response($response, 200); 

?> 

我在php(代碼Ignitor)中運行以上服務發送通知,但我沒有使用代碼點火器。雖然上述服務運行成功,但我沒有收到我手機上的通知。 任何人都可以告訴問題是什麼。

回答

0

這個工作對我來說:

 $ctx = stream_context_create(); 

     stream_context_set_option($ctx, 'ssl', 'local_cert', $certFilePath); 
     stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
     stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer'); 

     $webServiceLocation = 'tls://gateway.push.apple.com:2195'; 
     $fp = stream_socket_client(
       $webServiceLocation, $err, 
       $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

     if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); 

     $body['aps'] = array('alert' => $message, 'sound' => 'default'); 

     $payload = json_encode($body); 

     $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

     $result = fwrite($fp, $msg, strlen($msg)); 

     fclose($fp); 
+0

謝謝您的回答,我想這項服務,但仍以其運行沒有錯誤,但沒有顯示iphone – Guneet 2014-11-05 04:59:43

+0

的任何通知,那麼你必須檢查密碼和設備標識以確保它們對您正在測試的環境(sanbox,生產)是正確的 – matteo 2014-11-05 10:46:38