IPhone 消息推送实现

参考 资料 http://blog.csdn.net/victormokai/article/details/39501277

对生成pem 的补充

拿到mac 上生成导出的两个文件 cert.p12 , key.p12 放到一个文件夹下

终端切换到这个文件夹下

openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
生成cert.pem;输入访问密码123456,设置pem文件的密码123456(服务器访问pem的密码) openssl pkcs12 -nocerts -out key.pem -in key.p12
生成key.pem;操作同上 (可选)
openssl rsa -in key.pem -out key.unencrypted.pem如果需要对key不进行加密。 cat cert.pem key.unencrypted.pem > ck.pem
合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。

PHP 案例

<?php
/**
* PHP iospush
*
* Copyright (c) 2006 - 2014 iospush
*
*
* @category PHP iospush
* @copyright Copyright (c) iospush (http://www.leipi.org)
* @license http://www.apple.com LGPL
* @version 1.8.0, 2014-03-02
*/
class iospush
{
public $path='cert.pem';//证书
public $pass='linksame'; //密码
public $sound = 'tap.aif'; //ͨ声音
public $badge=0; public function __construct($path='',$pass='') {
if($path)
$this->path = $path;
if($pass)
$this->pass = $pass; }
function push($token, $data)
{ $ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->path);
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->pass); // Open a connection to the APNS server
//这个为正是的发布地址
//$fp = stream_socket_client('tls://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
//这个是沙盒测试地址,发布到appstore后记得修改哦
$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp){
return false;
} // Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
); $body['aps']['alert'] = $data['description'];
$body['aps']['sound'] = 'default';
foreach($data as $k => $v) {
if ($k != "description") {
$body[$k] = $v;
}
} // Encode the payload as JSON
$payload = json_encode($body); // Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $token)) . pack('n', strlen($payload)) . $payload; // Send it to the server
$result = fwrite($fp, $msg, strlen($msg)); if (!$result)
return false;
//echo 'Message not delivered' . PHP_EOL;
else
//echo 'Message successfully delivered' . PHP_EOL;
return true;
// Close the connection to the server
fclose($fp); // Construct the notification payload
// $body = array();
// if ($this->badge) {
// $body['aps']['badge'] = $this->badge;
// }
// $body['aps']['alert'] = $data['description'];
// $body['aps']['sound'] = $this->sound;
// // ��װ���
// foreach($data as $k => $v) {
// if ($k != "description") {
// $body[$k] = $v;
// }
// } // // $body['append_1']="appendent_1";
// // $body['append_2']="appendent_2";
// $ctx = stream_context_create();
// stream_context_set_option($ctx, 'ssl', 'local_cert', $path); //pem�ļ���ַ
// stream_context_set_option($ctx, 'ssl', 'passphrase', $this->pass); //֤������ // // $fp=stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);//��ʽ
// $fp = @stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //����ɳ�� // if (!$fp) {
// return false;
// } // $payload = json_encode($body);
// $msg = chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $token)) . pack("n", strlen($payload)) . $payload; // fwrite($fp, $msg);
// fclose($fp);
return true;
}
} $deviceToken='93f852218f8c911b40432999ab1d02280a491e8ed0d64a0178e2d7f82e827452'; $pp= new iospush('ck.pem','linksame');
$data['description']="回个哈哈哈";
$statusss=$pp->push($deviceToken, $data);
var_dump($statusss);
?>

最新文章

  1. 锋利的jQuery--Ajax(读书笔记四)
  2. 如何在maven工程中加载oracle驱动
  3. Android课程---Oracle VM VirtualBox出现不能为虚拟机打开一个新任务
  4. jquery中checkbox选中的问题之prop&amp;attr惹的祸
  5. MongoDB源码概述——内存管理和存储引擎
  6. 深入理解Javascript
  7. 基于华为Java编程规范的Eclipse checkStyle.xml
  8. tlplayer for wince 版本正式商用
  9. Bootstrap 样式定制-lessc编译源码
  10. 【c语言】模拟库函数strstr
  11. memcached命令和配置
  12. jmeter导入DB数据再再优化
  13. 网页 cookie
  14. Codeforces543BDestory Roads心得
  15. [HNOI2010]PLANAR
  16. first one
  17. Linux 下安装JDK和jmeter 及环境配置记录过程
  18. IIS应用程序池相关问题及连接池已满的解决方法
  19. ASP.NET MVC + MySQL で開発環境構築
  20. java(9)并发编程

热门文章

  1. pymongo的简单使用
  2. python项目开发视频
  3. js request学习
  4. Python开发:模块
  5. apache下虚拟域名配置
  6. lnmp环境的使用
  7. [android开发篇] android apidemo 学习网址
  8. HDU1007 TLE代码和AC代码对比
  9. OSQL 用法
  10. HDU 1071 The area ——微积分