iOS APNS推送Java、c# P12证书和Php pem证书制作

Java、c# P12证书:http://www.jianshu.com/p/4282…
Php pem证书制作:http://blog.csdn.net/dztianyu…

有可能出现的错误:

1.执行php push.php时报错 :

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Users/songhua/Desktop/推送证书文件/Php-Push/push.php on line 30

Warning: stream_socket_client(): Failed to enable crypto in /Users/songhua/Desktop/推送证书文件/Php-Push/push.php on line 30

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/songhua/Desktop/推送证书文件/Php-Push/push.php on line 30
Failed to connect 0 

解决方案:

去下载https://entrust2048.bj.bcebos… 网关文件并在push.php文件里加入stream_context_set_option($ctx,’ssl’,’cafile’,’entrust_2048_ca.cer’);

php推送代码,entrust_2048_ca.cer、pem和php必须在同一个目录下:

<?php  
//手机注册应用返回唯一的deviceToken  
// $deviceToken = 'c112ed059d3bc019833f9eb991cd9e594583381dd979b7a6f809f683efd70d82';  
$deviceToken = '0191498cf8704bf9c09a624855f0ad0e0b65933d33e9b296223745b22d8779c8';  
//ck.pem通关密码  
$pass = '123456';     
//消息内容  
$message = 'A test message!';  
//badge我也不知是什么  
$badge = 4;  
//sound我也不知是什么(或许是推送消息到手机时的提示音)  
$sound = 'Duck.wav';  
//建设的通知有效载荷(即通知包含的一些信息)  
$body = array();  
$body['id'] = "4f94d38e7d9704f15c000055";  
$body['aps'] = array('alert' => $message);  
if ($badge)  
  $body['aps']['badge'] = $badge;  
if ($sound)  
  $body['aps']['sound'] = $sound;  
//把数组数据转换为json数据  
$payload = json_encode($body);  
echo strlen($payload),"\r\n";  
  
//下边的写法就是死写法了,一般不需要修改,  
//唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195这个是沙盒测试地址,ssl://gateway.push.apple.com:2195正式发布地址  
$ctx = stream_context_create();  
// $pem = dirname(__FILE__) .'/'.'phpck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', 'phpck.pem');
stream_context_set_option($ctx,'ssl','cafile','entrust_2048_ca.cer');   
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
if (!$fp) {  
    print "Failed to connect $err $errstr\n";  
    return;  
}  
else {  
   print "Connection OK\n<br/>";  
}  
// send message  
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
print "Sending message :" . $payload . "\n";    
fwrite($fp, $msg);  
fclose($fp);  
?>  

    原文作者:宋冬野
    原文地址: https://segmentfault.com/a/1190000011895570
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞