IOS本地,APNS远程推送(具体过程)
添加本地推送
///本地添加
- -(void)addLocalPushNotification:(UIButton*)sender;
- {
- NSLog(@"%s",__FUNCTION__);
- UILocalNotification* localNotification=[[UILocalNotification alloc]init];
- if (localNotification) {
- //设置时间当前加20秒
- NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];
- /*推送时区设置:从网上搜到
- timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。*/
- localNotification.timeZone=[NSTimeZone defaultTimeZone];
- ///推送时间设置
- localNotification.fireDate=pushDate;
- //时间间隔,若不设置将只会推送1次
- localNotification.repeatInterval=kCFCalendarUnitDay;
- //推送时的声音,(若不设置的话系统推送时会无声音)
- localNotification.soundName=UILocalNotificationDefaultSoundName;
- //推送的文字信息(若不设置,推送中心中不显示文字,有声音提示前提是设置有声音)
- localNotification.alertBody=@"Hello world";
- //推送时小图标的设置,PS:这个东西不知道还有啥用
- localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
- ///这个东西,到时用于定位是哪个notification,以便取消用
- NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- localNotification.userInfo=infoDic;
- //讲推送设置以及信息加入
- UIApplication* app=[UIApplication sharedApplication];
- BOOL status=YES;
- for (UILocalNotification* notification in app.scheduledLocalNotifications) {
- if ([notification.userInfo objectForKey:@"key"]) {
- status=NO;
- }
- }
- if (status) {
- //加入推送(只能加入一次)
- [app scheduleLocalNotification:localNotification];
- }
- NSLog(@"%@",app.scheduledLocalNotifications);
- }
- }
取消本地推送
- ///本地移除
- -(void)removeLocalPushNotification:(UIButton*)sender
- {
- NSLog(@"%s",__FUNCTION__);
- UIApplication* app=[UIApplication sharedApplication];
- //获取当前应用所有的通知
- NSArray* localNotifications=[app scheduledLocalNotifications];
- if (localNotifications) {
- for (UILocalNotification* notification in localNotifications) {
- NSDictionary* dic=notification.userInfo;
- if (dic) {
- NSString* key=[dic objectForKey:@"key"];
- if ([key isEqualToString:@"name"]) {
- //取消推送 (指定一个取消)
- [app cancelLocalNotification:notification];
- break;
- }
- }
- }
- }
- //取消当前应用所有的推送
- //[app cancelAllLocalNotifications];
- }
远程推送
当服务端远程向APNS推送至一台离线的设备时,苹果服务器Qos组件会自动保留一份最新的通知,等设备上线后,Qos将把推送发送到目标设备上
客户端需要注意的
bundle ID与App Id一致
设备Token能正常获取
若为沙盒测试,证书得使用developer的
单设备
如上图所示:我们的服务端将需要推送的相关信息提交到APNS(Apple Push Notification Service),由APNS在Push服务IOS设备列表中找到对应的设备,并将信息推到终端上,终端上再推到客户端APP上
多设备
流程大概是这样的
1.生成CertificateSigningRequest.certSigningRequest文件
2.将CertificateSigningRequest.certSigningRequest上传进developer,导出.cer文件
3.利用CSR导出P12文件
4.需要准备下设备token值(无空格)
5.使用OpenSSL合成服务器所使用的推送证书
1.打开钥匙串,在右上角选择(钥匙串访问->证书助理->从证书颁发机构请求证书)
生成 CertificateSigningRequest.certSigningRequest
以下信息填写号后,保存到对应位置
2.进入developer.apple.com中 上传CertificateSigningRequest.certSigningRequest并保存cer文件
(1)
(2)选择类型为 推送服务--沙盒测试用
(3)选中对应的APP ID,别忘了,项目配置文件中的Bundle ID与其一致
(4)选择保存路径
(5)选择上传文件 CertificateSigningRequest.certSigningRequest
(6)保存cer文件,并双击添加进钥匙串
(7)新建一个Provisioning Profiles
选中与前面一致的 App Id
选中刚才新建的certificates
选择可调试设备
保存provisioning文件,并将其加入设备中
通过OPENSSL文件合并
1.在钥匙串->证书 找到刚才所添加进去的证书 右键导出p12
2.进入终端 ,将aps_development.cer转成PushChatCert.pem(openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem)
3.openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12 生成p12私钥 .pem文件(需设置密码,服务端推送时要用)
4.利用PushChatCert.pem和新生成的PushChatKey.pem合成一个新的p12文件(这个p12是提供给服务器推送用的)(
openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12
)
合成PHP所用的PEM文件
- openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
- openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
- cat PushChatCert.pem PushChatKey.pem > newck.pem
代码实现如下
注册推送通知
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
- (UIRemoteNotificationTypeAlert|
- UIRemoteNotificationTypeBadge|
- UIRemoteNotificationTypeSound)];
在AppDelegate中加入以下几个代理方法
- ///Token值成功获取的时候走的是这个方法(Token值不能带空格)
- -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- NSLog(@"%@",deviceToken);
- }
- ///Token值获取失败的时候走的是这个方法
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
- NSLog(@"%@",error);
- }
- ///应用程序处在打开状态,且服务器有推送消息过来时,以及通过推送打开应用程序,走的是这个方法
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- for (id key in userInfo) {
- NSLog(@"%@:%@",key, [userInfo objectForKey:key]);
- }
- ///Icon推送数量设为0
- // application.applicationIconBadgeNumber=0;
- }
应用程序不处在后台,且通过推送通知打开的时候,如果需要推送下来相关的信息可以在
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中加入
- ///应用程序不处在后台,并且是通过推送打开应用的时候
- if (launchOptions) {
- ///获取到推送相关的信息
- NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- }
服务端PHP推送代码
- <?php
- $deviceToken= 'ba6d5106503c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6'; //没有空格
- $body = array("aps" => array("alert" => 'helloHui',"badge" => 2,"sound"=>'default')); //推送方式,包含内容和声音
- $ctx = stream_context_create();
- //如果在Windows的服务器上,寻找pem路径会有问题,路径修改成这样的方法:
- //$pem = dirname(__FILE__) . '/' . 'apns-dev.pem';
- //linux 的服务器直接写pem的路径即可
- stream_context_set_option($ctx,"ssl","local_cert","26ck.pem");
- $pass = "123123";
- stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
- //此处有两个服务器需要选择,如果是开发测试用,选择第二名sandbox的服务器并使用Dev的pem证书,如果是正是发布,使用Product的pem并选用正式的服务器
- // $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) {
- echo "Failed to connect $err $errstrn";
- return;
- }
- print "Connection OK\n";
- $payload = json_encode($body);
- $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
- echo "sending message :" . $payload ."\n";
- fwrite($fp, $msg);
- fclose($fp);?>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <div style="padding-top:20px">
- <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
- </div>
IOS本地,APNS远程推送(具体过程)的更多相关文章
- iOS开发之远程推送
说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...
- iOS APNs远程推送流程精简版
1.去Apple Developer Center里创建应用的信息,指定APP ID(Bundle ID),配置里开启推送功能(Push Notifications). 后续步骤需要用到这个应用的包名 ...
- iOS APNS远程推送(史上最全步骤)
/*****************************************1************************************************/ waterma ...
- APNS远程推送(转发)
/*****************************************2************************************************/ /****** ...
- IOS 基于APNS消息推送原理与实现(JAVA后台)
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Pu ...
- 转:IOS 基于APNS消息推送原理与实现(JAVA后台)
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- iOS 基于APNS消息推送原理与实现(包括JAVA后台代码)
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- APNS 远程推送通知 PUSH deviceToken
服务器向客户端推送消息: 当应用程序推到后台,或者根本就没有运行(我们的代码无能为力) 如果这种情况之下,应用程序想和用户交互(传统的做法 不可能) 推送 APNS:Ap ...
随机推荐
- 读书笔记---PMBOK第五版官方中文版
以下是为了准备PMP考试时学习<PMBOK第五版官方中文版>这本书的笔记和摘要,目的是为了以后可以快速的抓住本书的核心重点复习. 引论 PMPOK的目的 收录了项目管理知识体系中被普遍认可 ...
- duilib学习 --- 360demo 学习
我想通过360demo的学习,大概就能把握duilib的一般用法,同时引申出一些普遍问题,和普遍解决方法.并在此分享一些链接和更多内容的深入学习..... 原谅我是一个菜鸟,什么都想知道得清清楚楚.. ...
- CSS编写技巧
1.尽量少的使用全局的重置代码 全局重置代码:*{margin:0; padding:0;}对于熟悉CSS的人来说并不陌生,并且有很多人的第一句CSS代码就是这句.它可以避免不同浏览器的默认间距不同而 ...
- Mysql查看执行计划-explain
最近生产环境有一些查询较慢,需要优化,于是先进行业务确认查询条件是否可以优化,不行再进行sql优化,于是学习了下Mysql查看执行计划. 语法 explain <sql语句> 例如: e ...
- Xshell远程管理Linux
Xshell[1]是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议. Xshell 通过互联网到远程主机的安全连接以及它创新 ...
- angular指令深度学习篇
angular指令深度学习-过滤器 limitTo ... <body ng-app="app" > <div ng-controller="myCtr ...
- Linux 查找文件并删除文件内容
find * ./ |while read file; do echo ''>$file; done
- K米点歌APP评测
K米APP评测 产品简介 K米点歌是一款免费的社交K歌手机应用,其手机点歌功能主要在KTV.夜总会,酒吧等K歌场所中使用,当前提供iPhone版本及安卓版本下载使用.——百度百科 评测版本 K米点歌4 ...
- php网址显示excel表格内容
/** * excel表格内容在网页中显示 * * 首先需要下载PHPExcel 工具包 * 网址: http://phpexcel.codeplex.com/releases/view/119187 ...
- WebService学习总结(四)——调用第三方提供的webService服务
http://www.cnblogs.com/xdp-gacl/p/4260627.html 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他 ...