2014-02-25 94 views
8

我是一名iOS應用程序開發人員,對PHP不太瞭解。我正在關注raywenderlich push notification tutorial以生成推送通知的SSL證書和私鑰。iOS推送通知發送錯誤

我按照步驟3-4次,但它不起作用。我之前採用了相同的步驟,並且工作正常。

我認爲問題出在我測試使用SSL證書連接到APNS服務器時得到的響應中。我得到了低於答覆。

CONNECTED(00000003) 
depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C 
verify error:num=20:unable to get local issuer certificate 
verify return:0 
--- 
Certificate chain 
0 s:/C=US/ST=California/L=Cupertino/O=Apple Inc./OU=iTMS Engineering/CN=gateway.sandbox.push.apple.com 
    i:/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C 
1 s:/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C 
    i:/O=Entrust.net/OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.)/OU=(c) 1999 Entrust.net Limited/CN=Entrust.net Certification Authority (2048) 
--- 
Server certificate 
-----BEGIN CERTIFICATE----- 
MIIFGzCCBAOgAwIBAgIETBz90jANBgkqhkiG9w0BAQUFADCBsTELMAkGA1UEBhMC 
VVMxFjAUBgNVBAoTDUVudHJ1c3QsIEluYy4xOTA3BgNVBAsTMHd3dy5lbnRydXN0 
Lm5ldC9ycGEgaXMgaW5jb3Jwb3JhdGVkIGJ5IHJlZmVyZW5jZTEfMB0GA1UECxMW 
KGMpIDIwMDkgRW50cnVzdCwgSW5jLjEuMCwGA1UEAxMlRW50cnVzdCBDZXJ0aWZp 
Y2F0aW9uIEF1dGhvcml0eSAtIEwxQzAeFw0xMjA1MjUyMzM3NDZaFw0xNDA1MzEw 
NTA4NDhaMIGPMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2Fsa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4IBAQAS 
EDkUyBHVdRJnCLHY8w9ec92NWqBYqKiSGP0uVCvgpsJIWDBkCGIw1Olks6mQuS9+ 
R7VRJJFg7EhtufmoRIvjgntKpTe49sB/lrmiZVQGnhjd6YdyYm9+OBUWRvwketLM 
v0S+nxZD0qLLJ9foVUB8zP8LtutqFJ5IZw1xb9eSNzhpKkQ9ylj8MCd4tpXZxICL 
Gt327poTXwmjQ+31fz7HCQCowMHccP8kiKM5SeYC9q+nkmdaozHVvw4e1RsP+EWO 
vPtcH1x1BCkTJajmrO7JuRPLuBEnZGSPUVFRKWP9jy0a28VnJek+oA7rRMRD8irU 
fMGbLqkGn8YogdPqe5T1 
-----END CERTIFICATE----- 
subject=/C=US/ST=California/L=Cupertino/O=Apple Inc./OU=iTMS Engineering/CN=gateway.sandbox.push.apple.com 
issuer=/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C 
--- 
No client certificate CA names sent 
--- 
SSL handshake has read 2731 bytes and written 2177 bytes 
--- 
New, TLSv1/SSLv3, Cipher is AES256-SHA 
Server public key is 2048 bit 
Secure Renegotiation IS supported 
Compression: NONE 
Expansion: NONE 
SSL-Session: 
    Protocol : TLSv1 
    Cipher : AES256-SHA 
    Session-ID: 
    Session-ID-ctx: 
    Master-Key: 0E813189A9AA0F93F3C996DB3D80240F742EB24656AEED9DC18043DCEDD854E9B1C4798098312EB7F6CAB23B10FF343C 
    Key-Arg : None 
    Start Time: 1393333259 
    Timeout : 300 (sec) 
    Verify return code: 0 (ok) 


在反應開始提示錯誤verify error:num=20:unable to get local issuer certificate。在後面的迴應部分中還說No client certificate CA names sent。 我不知道這些事情,但我懷疑問題是與這些錯誤。

編輯:我使用教程中提供的示例php腳本發送通知時出現錯誤。

Warning: stream_socket_client(): Unable to set private key file `/Users/akashpatel/Desktop/SimplePush/ck.pem' in /Users/akashpatel/Desktop/SimplePush/simplepush.php on line 25 

Warning: stream_socket_client(): failed to create an SSL handle in /Users/akashpatel/Desktop/SimplePush/simplepush.php on line 25 

Warning: stream_socket_client(): Failed to enable crypto in /Users/akashpatel/Desktop/SimplePush/simplepush.php on line 25 

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/akashpatel/Desktop/SimplePush/simplepush.php on line 25 
Failed to connect: 0 

編輯: 使用此app(由塞格夫在他的回答建議)我測試是否與證書/私鑰的任何問題。我可以成功收到通知。所以我想問題不在於證書/私鑰。

PHP腳本我使用:

<?php 

$deviceToken = 'r34f34f5g45g5y56u76hj676elfjn4urno43f958gh945g8g7'; 

// Put your private key's passphrase here: 
$passphrase = ‘pasphrase’; 

// Put your alert message here: 
$message = 'Received push notification!'; 

//////////////////////////////////////////////////////////////////////////////// 

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', '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); 

echo '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) 
    echo 'Message not delivered' . PHP_EOL; 
else 
    echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

編輯:,當我在生產服務器上使用我的腳本工作。但是我會保持這個問題的開放性,因爲它在本地服務器上不起作用。建議可能有助於其他人在生產服務器上不工作。

+0

什麼是你的ssl命令?您是否嘗試過:openssl s_client -connect host:443 -cert cert_and_key.pem -key cert_and_key.pem -state -debug – Radu

+0

@Radu cert_and_key.pem在您的命令中指定了兩次。這是正確還是錯誤?我使用命令'openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert Cert.pem -key Key.pem'。 – Geek

+0

@Radu我試着改變端口到'443'並出錯。 'connect:Connection refused,connect:errno = 61'。 – Geek

回答

1

我不知道當我使用我在本地服務器上跟隨的教程提供的PHP腳本進行測試時出現了什麼問題。但是當我使用生產服務器進行測試時,它正在發送通知。

3

推送通知出現了很多問題,您需要逐個處理它們。

忘掉一秒鐘的php代碼。 轉到here並下載最新版本。

這是一個偉大的小應用程序,爲您做所有的服務器端並將發送推送通知到您的設備。如果可行,您可以繼續並執行php代碼,但我懷疑您的問題在於應用程序證書\密鑰。

+1

我下載了應用程序並使用相同的證書發送了通知。我收到通知。現在它意味着證書和密鑰都很好。什麼可以是其他問題?順便說一句,感謝和+1鏈接到應用程序。至少我知道我不需要再創建它們來查看是否有問題。 – Geek

+0

非常感謝你的小應用程序測試設置:) – Nitin

5

請參閱錯誤:

CONNECTED(00000003) depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C verify error:num=20:unable to get local issuer certificate verify return:0

看你如何設置應用的上下文流選項:

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

我用同樣的教程爲你,在troobleshooting部讀出的子彈#3

Unable to get local issuer certificate. This error means that the certificate from the server could not be verified. To fix this you need to download the Entrust CA root certificate. This can be done from the Terminal using the command: curl -O https://www.entrust.net/downloads/binary/entrust_2048_ca.cer You then also need to add stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

下載entrust_2048_ca.cer

確保您的所有證書都與PHP腳本位於同一目錄中。

你的代碼更改爲以下:

//applying context to stream option 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer'); 

您現在應該是不錯的。

+0

我應該提到我的問題。我已經嘗試過了。但不工作。 – Geek

+0

你可以[聊天](http://chat.stackoverflow.com/rooms/48430/discussion-between-geek-and-radu) – meda

+0

是的,我可用。 – Geek