ios推送服务,php服务端
本文转载至http://my.oschina.net/AStar/blog/176531
证书生成参考:https://parse.com/tutorials/ios-push-notifications
会生成三个文件
xxx.cer
xxx.certSigningRequest
xxx.p12
用PHP做推送需要
推送证书生成。
生成pem步骤:
1.下载下来你配置好的推送证书aps_developer_identity.cer 文件。
2.转换 .cer 文件到 .pem 文件:
openssl x509 -in aps_developer_identity.cer -inform der -out PushChatCert.pem
3.在把你“钥匙”推送证书导出成的.p12到.pem文件:
openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12
Enter Import Password:
MAC verified OK
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
4.合并两个pem文件:cat PushChatCert.pem PushChatKey.pem > ck.pem
5.测试证书是否可用:
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem
****************客户端推送处理,以下在AppDelegate文添加****************
//是否支持注册推送
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/*............... */
//消息推送支持的类型
UIRemoteNotificationType types =(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert);
//注册消息推送
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:types];
}
发送推送的思路:先获取手机的 deviceToken ,然后发送到我们的服务器
//获取DeviceToken成功
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"注册device token");
NSLog(@"DeviceToken: {%@}",deviceToken);
//这里进行的操作,是将Device Token发送到服务端
NSString *tokenStr = [deviceToken description];
NSString *pushToken = [[[tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
//注册到我们的服务器
NSString *url = @"http://api.xxxxxxcom/index.php?r=site/saveIphoneDeviceToken&device_token=";
url = [url stringByAppendingFormat:@"%@", pushToken];
[[AFOSCClient sharedClient]getPath:url parameters:Nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"注册成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"注册失败");
}];
}
然后是处理接收推送消息
//注册消息推送失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"注册消息推送失败");
NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);
}
//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// NSLog(@"Receive remote notification : %@",userInfo);
// NSLog(@"%@",[userInfo objectForKey:@"aps"]);
NSDictionary *aps = [userInfo objectForKey:@"aps"];
// NSLog(@"%@",[aps objectForKey:@"alert"]);
NSString *msg = [aps objectForKey:@"alert"];
// NSLog(@"%@",[userInfo objectForKey:@"alert"]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"温馨提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;//把badge设置为0
}
****************服务器端push.php****************
<?php
//手机注册应用返回唯一的deviceToken
$deviceToken = 'eb87b209 6395a040 a1cf85e5 15b6e972 20ec883f 6b3ac80f 02b92a1a 58cf273e';
//ck.pem通关密码
$pass = '13!@#23';
//消息内容
$message = '这是一条推送测试通知1'.time();
//badge我也不知是什么
//$badge = 1;
//sound我也不知是什么(或许是推送消息到手机时的提示音)
$sound = 'Duck.wav';
//建设的通知有效载荷(即通知包含的一些信息)
$body = array();
$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();
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__) . '\\' . 'dev_ck.pem');
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);
?>
php服务端如果出问题,请检查是否开通ssl模块
Apache需要开启ssl模块,通过查看Apache的官方文档得知,使用ssl需要Apache开启三个支持模块分别是:
mod_include
mod_cgi
mod_expires
*注意:ios 的推送消息有256个字符长度限制;超出范围不能发送
ios推送服务,php服务端的更多相关文章
- IOS 推送消息 php做推送服务端
IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPh ...
- 转载:iOS 推送的服务端实现
参考网址1: iOS消息推送机制的实现 http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html 参考网址2: iOS 推送的 ...
- iOS推送服务细节回顾
iOS推送服务细节回顾 之前在做推送功能时候,就总结过一系列证书的制作,OC代码实现和服务器搭建等经验.又过了一段时间了,前前后后对推送服务做了多次的完善和优化,有iOS客户端的,还有本地服务器端的. ...
- iOS 推送全解析
本文旨在对 iOS 推送(以下简称 推送)进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacod ...
- iOS推送通知的实现步骤
一.关于推送通知 来源:http://blog.csdn.net/enuola/article/details/8627283 推送通知,也被叫做远程通知,是在iOS 3.0以后被引入的功能.是当程序 ...
- 一步一步教你做ios推送
最近在研究ios的推送问题,遇到了一些问题,最终整理了一下.放在这里和大家分享 APNS的推送机制 首先我们看一下苹果官方给出的对ios推送机制的解释.如下图 Provider就是我们自己程序的后台服 ...
- iOS 推送全解析,你不可不知的所有 Tips!
本文旨在对 iOS 推送进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacode 使用 iOS ...
- CC03 iOS推送机制浅析
• ios推送机制 可以通俗的把APNS理解为iOS系统为每个app提供的长连接通道 苹果限制了每个app在后台存活的时间,最重要的目的是为了省电,其次优化内存这些.如果彻彻底底的将app杀死了,服务 ...
- 友盟iOS推送配置(从真机调试到推送)
下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...
- iOS推送失败的可能问题汇总
ITC上的证书问题 AppID未开启推送 Provioning Profile在AppID开启推送功能前生成的 Provioning证书过期 推送的pem证书过期 客户端问题 target的CodeS ...
随机推荐
- adb 设备不识别
android真坑 有两台测试机 都能连能snapdragon profiling了 忽然 一台不能识别了 adb devices 就不存在 一台一直是好的 kill server start ser ...
- 关于hibernate中映射中有many to one等外键关联时的问题
hibernate中的对象的3种状态的理解及导致报错object references an unsaved transient instance - save the transient insta ...
- ambari journalnode异常Can't scan a pre-transactional edit log
今天在删日志文件,不知道删错哪个地方了. 该目录下一直报错,这个日志文件增长很快, /var/log/hadoop/hdfs/ hadoop-hdfs-journalnode-xx.log 先备份/h ...
- 2017.4.18 putty和fileZilla的使用
putty:用来连接环境. fileZila:用来传递文件. (1)连接环境 centOS 7 点击putty.exe,输入地址.用户名.密码进行连接.端口输入22.用账号和密码登录. 进入到目录下, ...
- 在vs2010中编译log4cxx-0.10.0具体方法(从下载、编译、解决错误具体介绍)
一. 简单介绍 log4cxx是Java社区著名的log4j的c++移植版.用于为C++程序提供日志功能,以便开发人员对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于A ...
- UltraISO 9.6.5.3237
注册信息: 用户名:Guanjiu 注册码:A06C-83A7-701D-6CFC
- Yii 获得当前控制器和方法
[怎样获得当前控制器和方法] 控制器:$this -> id ; 方法:$this->action->id ; 这主要是用在视图中,进行高亮显示. <div id=" ...
- web-压力测试学习
https://blog.linuxeye.com/335.html http://bdql.iteye.com/blog/291987 http://www.cnblogs.com/zhuque/a ...
- PS 流格式解析(转)
对于PS流,最近因为工作需要,所以MPEG2中的PS流格式和解包过程进行了学习. 首先我们需要知道PS包流格式是怎么样的: 针对H264 做如下PS 封装:每个IDR NALU 前一般都会包含SPS. ...
- slam command tool
cd imu_ws source devel/setup.bash ls -l /dev |grep ttyUSB sudo chmod /dev/ttyUSB0 rosrun imu_pb imu ...