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);
?>

iphone 消息推送 实现的更多相关文章

  1. 关于iphone消息推送把C#当服务器端来发送

    看了苹果消息推送文档,感觉推送很简单的,但是还是按个人习惯把这些简单知识记录下来,在需要时候再查看一下! 在开发之前,要准备以下的资料 1.证书(包括产生证书和调试证书) 2.证书密码 3.唯一标识( ...

  2. 分分钟搞定IOS远程消息推送

    一.引言 IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在http://my.oschina.net/u/2340880/blog/405491这篇博客中有详细的介绍,这里主要讨论远 ...

  3. [Erlang 0106] Erlang实现Apple Push Notifications消息推送

        我们的IOS移动应用要实现消息推送,告诉用户有多少条消息未读,类似下图的效果(笑果),特把APNS和Erlang相关解决方案笔记于此备忘.          上面图片中是Apple Notif ...

  4. Push:iOS基于APNS的消息推送

    1. Push的三个步骤,如下图所示: (1)Push服务应用程序把要发送的消息.目的iPhone的标识打包,发给APNS: (2)APNS在自身的已注册Push服务的iPhone列表中,查找有相应标 ...

  5. IOS - 消息推送原理和实现

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...

  6. IOS开发之实现App消息推送

    转自:http://blog.csdn.net/shenjie12345678/article/details/41120637 第一部分 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps: ...

  7. Android消息推送怎么实现?

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  8. 【转】APNs消息推送完整讲解

    https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificat ...

  9. .NET向APNS苹果消息推送通知

    一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...

随机推荐

  1. php数据查询之基础查询

    ---恢复内容开始--- 数据查询语言(Data Query Language) 基本查询 语法形式: select [all | distinct ] 字段或者表达式列表 [from子句] [whe ...

  2. STM32开发笔记之——CMSIS DAP

    都说开发stm32都是使用kail iar+jatg/swd的方式,然而arm公司已经开发出了CMSIS DAP的开源下载工具,全称是CoreSight Debug Access Port,网络上有大 ...

  3. POJ:2753-Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...

  4. eclipse中tab键设置

    1.点击 window->preference-,依次选择 General->Editors->Text Editors,选中右侧的 insert space for tabs;如下 ...

  5. 用Go编写的本地文件服务器

    本文来自网易云社区,转载务必请注明出处. 一.前言 一切问题的起源就是来自一个问题"为什么我打的jar包没有注解?",带着这个疑问查了一圈资料,原来问题主要是在没有将源码中的注释进 ...

  6. [android开发篇] [应用组件]Intent 和 Intent 过滤器

    https://developer.android.com/guide/components/intents-filters.html Intent 是一个消息传递对象,您可以使用它从其他应用组件请求 ...

  7. Educational Codeforces Round 24

    A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...

  8. lvs+keepalive主从和主主架构

    下面配置主从 1)关闭SELinux和防火墙 vi /etc/sysconfig/selinux SELINUX=disabled setenforce 临时关闭SELinux,文件配置后,重启生效 ...

  9. nginx 的日志切割

    nginx的日志切割脚本 说明:在nginx的配置文件中nginx.conf并没有定义access_log的位置, 在/usr/local/nginx/conf/vhost/下的配置文件中定义了acc ...

  10. Linux 查看端口占用并杀掉进程

    1. 查看端口号占用情况: netstat -apn|grep 11305 tcp        0      0 10.65.42.27:80              172.22.142.20: ...