我是一名iOS应用开发者,对php不太了解。我正在关注raywenderlich push notification tutorial以生成推送通知的SSL证书和私钥。
我按照步骤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
NTA4NDhaMIGPMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTESMBAG
A1UEBxMJQ3VwZXJ0aW5vMRMwEQYDVQQKEwpBcHBsZSBJ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Fg7EhtufmoRIvjgntKpTe49sB/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(segev在他的回答中建议)我测试了证书/私钥是否存在任何问题。我可以成功收到通知。所以我猜问题不在于证书/私钥。
我正在使用的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 :(得分:4)
推送通知有很多问题,你需要逐一接近它们。
暂时忘掉php代码。 转到here并下载最新版本。
这是一款非常棒的小应用程序,可以为您完成所有服务器端,并会向您的设备发送推送通知。如果可行,你可以继续实现php代码,但我怀疑你的问题在于app certificate \ key。
答案 1 :(得分:3)
查看错误:
CONNECTED(00000003)depth = 1 / C = US / O =委托, Inc./OU=www.entrust.net/rpa通过引用并入/ OU =(c)2009 Entrust,Inc. / CN = Entrust证书颁发机构 - L1C验证 错误:num = 20:无法获取本地颁发者证书验证返回: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
无法获得本地颁发者证书。这个错误意味着 无法验证来自服务器的证书。解决这个问题 需要下载Entrust CA根证书。这可以做到 从终端使用命令:curl -O https://www.entrust.net/downloads/binary/entrust_2048_ca.cer然后你 还需要添加stream_context_set_option($ ctx,'ssl','cafile', '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');
你现在应该好。
答案 2 :(得分:1)
当我使用本地服务器上的教程提供的PHP脚本进行测试时,我不知道是什么问题。但是,当我使用生产服务器测试时,它正在发送通知。