ios php 聊天_iOS之消息推送(终端运行php推送)

一、先在工程里配置好证书,准备好这些相关文件:

a、Push.certSigningRequest

b、Push.p12

c、aps_developer_identity.cer

二、在应用服务器采用PHP的方式将消息推送给APNS

php连接APNS也是需要证书的,还记得我们上面获得的几个证书吗?打开终端,对上面的证书做如下处理

1. cd 进入证书所在目录 把.cer文件转换成.pem文件:

$ openssl x509 -in aps_developer_identity.cer -inform der

-out PushChatCert.pem

2. 把私钥Push.p12文件转换成.pem文件:

$ openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying – Enter PEM pass phrase:

你首先需要为.p12文件输入passphrase密码短语,这样OpenSSL可以读它。然后你需要键入一个新的密码短语来加密PEM文件。还是使用”pushchat”来作为PEM的密码短语。你需要选择一些更安全的密码短语。

注意:如果你没有键入一个PEM passphrase,OpenSSL将不会返回一个错误信息,但是产生的.pem文件里面将不会含有私钥。

3. 把私钥和证书整合到一个.pem文件里:

$ cat PushChatCert.pem PushChatKey.pem > ck.pem

为了测试证书是否工作,执行下面的命令:

$ telnet gateway.sandbox.push.apple.com 2195

Trying 17.172.232.226…

Connected to gateway.sandbox.push-apple.com.akadns.NET.

Escape character is ‘^]’.

它将尝试发送一个规则的,不加密的连接到APNS服务。如果你看到上面的反馈,那说明你的MAC能够到达APNS。按下Ctrl+C 关闭连接。如果得到一个错误信息,那么你需要确保你的防火墙允许2195端口。

然后再次连接,这次用我们的SSL证书和私钥来设置一个安全的连接:

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195

-cert PushChatCert.pem -key PushChatKey.pem

Enter pass phrase for PushChatKey.pem:

你会看到一个完整的输出,让你明白OpenSSL在后台做什么。如果连接是成功的,你可以键入一些字符。当你按下回车后,服务就会断开连接。如果在建立连接时有问题,OpenSSL将会给你一个错误消息,

ck.pem文件就是我们需要得到php连接APNS 的文件,将ck.pem和push.php放入同一目录上传到服务器,

4. push.php的代码如下:

// Put your device token here (without spaces):

$deviceToken = 'e54a28777d6e8cdf8b39e01b1362adad1dc54ee2e3cf5658eb3036a0914e009c';

// Put your private key's passphrase here:密语

$passphrase = '1yyg';

// Put your alert message here:

$message = '恭喜你中奖了。。啊哈哈哈哈,推送测试而已';

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', '1yyg_dev.pem');

// stream_context_set_option($ctx, 'ssl', 'local_cert', '1yyg_pro.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,

// 'ssl://gateway.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);

?>

三、终端执行php代码

命令:cd 进入到php的目录文件下

php push.php

你会发现出现推送成功的提示。。。当然是英文版。。

有什么问题或者疑问欢迎底下留言。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值