iphone 消息推送 实现
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 消息推送 实现的更多相关文章
- 关于iphone消息推送把C#当服务器端来发送
看了苹果消息推送文档,感觉推送很简单的,但是还是按个人习惯把这些简单知识记录下来,在需要时候再查看一下! 在开发之前,要准备以下的资料 1.证书(包括产生证书和调试证书) 2.证书密码 3.唯一标识( ...
- 分分钟搞定IOS远程消息推送
一.引言 IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在http://my.oschina.net/u/2340880/blog/405491这篇博客中有详细的介绍,这里主要讨论远 ...
- [Erlang 0106] Erlang实现Apple Push Notifications消息推送
我们的IOS移动应用要实现消息推送,告诉用户有多少条消息未读,类似下图的效果(笑果),特把APNS和Erlang相关解决方案笔记于此备忘. 上面图片中是Apple Notif ...
- Push:iOS基于APNS的消息推送
1. Push的三个步骤,如下图所示: (1)Push服务应用程序把要发送的消息.目的iPhone的标识打包,发给APNS: (2)APNS在自身的已注册Push服务的iPhone列表中,查找有相应标 ...
- IOS - 消息推送原理和实现
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- IOS开发之实现App消息推送
转自:http://blog.csdn.net/shenjie12345678/article/details/41120637 第一部分 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps: ...
- Android消息推送怎么实现?
在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...
- 【转】APNs消息推送完整讲解
https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificat ...
- .NET向APNS苹果消息推送通知
一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...
随机推荐
- R语言基础-data.frame
data.frame比较像表格,每一列是一个向量,即每列中的元素是同一类型:所有列具有相同的长度. x = 10:1 y = -4:5 q = c("Ha","oh&qu ...
- Hi3519V101 Uboot和Kernel编译
前面已经搭建好了Ubuntu下的海思开发环境,现在对编译Uboot和Kernel的过程做一个简单的记录.参考文档<Hi3519V101 U-boot 移植应用开发指南.pdf>和<H ...
- Struts 2 动作注释 配置动作扩展 全局开关
动作注释package com.yiibai.user.action; import org.apache.struts2.convention.annotation.Action; import o ...
- luogu1231 教辅的组成
注意把书拆成两份 #include <iostream> #include <cstring> #include <cstdio> #include <que ...
- Ubuntu14.04使用root登陆帐户
http://jingyan.baidu.com/article/27fa73268144f346f8271f83.html 1.输入sudo gedit /usr/share/lightdm/lig ...
- CI - Set CSRF Hash and Cookie
/** * Set CSRF Hash and Cookie * * @return string */ protected function _csrf_set_hash() { if ($this ...
- Charles-安装和配置
一. 安装.破解charles工具 1. 安装压缩包中的charles_setup.exe,安装完成后先不启动charles. 2. 在安装文件中找到crack文件,将文件中的charles.jar拷 ...
- Leetcode 337.大家结舍III
打家劫舍III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根".除了"根"之外,每栋房子有且只有 ...
- POJ-2594 Treasure Exploration,floyd+最小路径覆盖!
Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...
- Python2.6.6升级2.7.3
Python2.7替换2.6: 1.下载Python-2.7.3 #wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 2.解压 ...