添加本地推送

///本地添加

  1. -(void)addLocalPushNotification:(UIButton*)sender;
  2. {
  3. NSLog(@"%s",__FUNCTION__);
  4. UILocalNotification* localNotification=[[UILocalNotification alloc]init];
  5. if (localNotification) {
  6. //设置时间当前加20秒
  7. NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];
  8. /*推送时区设置:从网上搜到
  9. timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。*/
  10. localNotification.timeZone=[NSTimeZone defaultTimeZone];
  11. ///推送时间设置
  12. localNotification.fireDate=pushDate;
  13. //时间间隔,若不设置将只会推送1次
  14. localNotification.repeatInterval=kCFCalendarUnitDay;
  15. //推送时的声音,(若不设置的话系统推送时会无声音)
  16. localNotification.soundName=UILocalNotificationDefaultSoundName;
  17. //推送的文字信息(若不设置,推送中心中不显示文字,有声音提示前提是设置有声音)
  18. localNotification.alertBody=@"Hello world";
  19. //推送时小图标的设置,PS:这个东西不知道还有啥用
  20. localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
  21. ///这个东西,到时用于定位是哪个notification,以便取消用
  22. NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
  23. localNotification.userInfo=infoDic;
  24. //讲推送设置以及信息加入
  25. UIApplication* app=[UIApplication sharedApplication];
  26. BOOL status=YES;
  27. for (UILocalNotification* notification in app.scheduledLocalNotifications) {
  28. if ([notification.userInfo objectForKey:@"key"]) {
  29. status=NO;
  30. }
  31. }
  32. if (status) {
  33. //加入推送(只能加入一次)
  34. [app scheduleLocalNotification:localNotification];
  35. }
  36. NSLog(@"%@",app.scheduledLocalNotifications);
  37. }
  38. }

取消本地推送

  1. ///本地移除
  2. -(void)removeLocalPushNotification:(UIButton*)sender
  3. {
  4. NSLog(@"%s",__FUNCTION__);
  5. UIApplication* app=[UIApplication sharedApplication];
  6. //获取当前应用所有的通知
  7. NSArray* localNotifications=[app scheduledLocalNotifications];
  8. if (localNotifications) {
  9. for (UILocalNotification* notification in localNotifications) {
  10. NSDictionary* dic=notification.userInfo;
  11. if (dic) {
  12. NSString* key=[dic objectForKey:@"key"];
  13. if ([key isEqualToString:@"name"]) {
  14. //取消推送 (指定一个取消)
  15. [app cancelLocalNotification:notification];
  16. break;
  17. }
  18. }
  19. }
  20. }
  21. //取消当前应用所有的推送
  22. //[app cancelAllLocalNotifications];
  23. }

远程推送

当服务端远程向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文件

  1.   openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
  2. openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
  3. cat PushChatCert.pem PushChatKey.pem > newck.pem

代码实现如下

注册推送通知

  1. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
  2. (UIRemoteNotificationTypeAlert|
  3. UIRemoteNotificationTypeBadge|
  4. UIRemoteNotificationTypeSound)];

在AppDelegate中加入以下几个代理方法

  1. ///Token值成功获取的时候走的是这个方法(Token值不能带空格)
  2. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  3. {
  4. NSLog(@"%@",deviceToken);
  5. }
  6. ///Token值获取失败的时候走的是这个方法
  7. -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  8. {
  9. NSLog(@"%@",error);
  10. }
  11. ///应用程序处在打开状态,且服务器有推送消息过来时,以及通过推送打开应用程序,走的是这个方法
  12. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  13. {
  14. for (id key in userInfo) {
  15. NSLog(@"%@:%@",key, [userInfo objectForKey:key]);
  16. }
  17. ///Icon推送数量设为0
  18. //    application.applicationIconBadgeNumber=0;
  19. }

应用程序不处在后台,且通过推送通知打开的时候,如果需要推送下来相关的信息可以在

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法中加入

  1. ///应用程序不处在后台,并且是通过推送打开应用的时候
  2. if (launchOptions) {
  3. ///获取到推送相关的信息
  4. NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  5. }

服务端PHP推送代码

  1. <?php
  2. $deviceToken= 'ba6d5106503c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6'; //没有空格
  3. $body = array("aps" => array("alert" => 'helloHui',"badge" => 2,"sound"=>'default'));  //推送方式,包含内容和声音
  4. $ctx = stream_context_create();
  5. //如果在Windows的服务器上,寻找pem路径会有问题,路径修改成这样的方法:
  6. //$pem = dirname(__FILE__) . '/' . 'apns-dev.pem';
  7. //linux 的服务器直接写pem的路径即可
  8. stream_context_set_option($ctx,"ssl","local_cert","26ck.pem");
  9. $pass = "123123";
  10. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
  11. //此处有两个服务器需要选择,如果是开发测试用,选择第二名sandbox的服务器并使用Dev的pem证书,如果是正是发布,使用Product的pem并选用正式的服务器
  12. //    $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  13. $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  14. if (!$fp) {
  15. echo "Failed to connect $err $errstrn";
  16. return;
  17. }
  18. print "Connection OK\n";
  19. $payload = json_encode($body);
  20. $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
  21. echo "sending message :" . $payload ."\n";
  22. fwrite($fp, $msg);
  23. fclose($fp);?>
  1. <pre></pre>
  2. <pre></pre>
  3. <pre></pre>
  4. <pre></pre>
  5. <pre></pre>
  6. <pre></pre>
  7. <div style="padding-top:20px">
  8. <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
  9. </div>
 
 

IOS本地,APNS远程推送(具体过程)的更多相关文章

  1. iOS开发之远程推送

    说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...

  2. iOS APNs远程推送流程精简版

    1.去Apple Developer Center里创建应用的信息,指定APP ID(Bundle ID),配置里开启推送功能(Push Notifications). 后续步骤需要用到这个应用的包名 ...

  3. iOS APNS远程推送(史上最全步骤)

    /*****************************************1************************************************/ waterma ...

  4. APNS远程推送(转发)

    /*****************************************2************************************************/ /****** ...

  5. IOS 基于APNS消息推送原理与实现(JAVA后台)

    Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Pu ...

  6. 转:IOS 基于APNS消息推送原理与实现(JAVA后台)

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  7. IOS 基于APNS消息推送原理与实现(JAVA后台)--转

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  8. iOS 基于APNS消息推送原理与实现(包括JAVA后台代码)

    Push的原理: Push 的工作机制可以简单的概括为下图   图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...

  9. APNS 远程推送通知 PUSH deviceToken

    服务器向客户端推送消息:      当应用程序推到后台,或者根本就没有运行(我们的代码无能为力)      如果这种情况之下,应用程序想和用户交互(传统的做法 不可能)      推送 APNS:Ap ...

随机推荐

  1. VPN使用指南|稳定的VPN|

    之前一直用GoagentFQ,但是有时不太稳定,网速也不是很快,然后就试用了云梯VPN,感觉还不错,网速挺快,也比较稳定.http://opticalvpn.com/?r=72ec52481ab8d2 ...

  2. linux终端常用快捷键

    Ctrl + d       删除一个字符,相当于通常的Delete键(命令行若无任何字符,则相当于exit:处理多行标准输入时也表示EOF ) Ctrl + h       退格删除一个字符,相当于 ...

  3. java学习指南

    @Override public BaseResponse selectListPage(PageData page, Object obj) { BaseResponse response = ne ...

  4. 使用 antd Table组件, 异步获取数据

    使用React.js + Redux + antd 制作CMS 后台内容管理系统,分享一点点积累,欢迎讨论. 在this.state中初始化数据: this.state = { pageNum:1, ...

  5. C#中中文编码的问题(StreamWriter和StreamReader默认编码)

    在使用StreamWriter和StreamReader时产生了这样的疑问,在不指定的情况下,他们使用什么编码方式? 查看MSDN,请看下图: 注意红色区域  这让我以为构造函数参数不同时使用不一样的 ...

  6. 什么是Javascript Hoisting?

    Javascript是一门容易遭人误解的语言,但是它的强大毋庸置疑.个人觉得,要想深入理解Javascript语言,首先必须对其基本的概念(例如:Scope,Closure,Hoisting等)要真正 ...

  7. 迅雷9、迅雷极速版之迅雷P2P加速:流量吸血鬼?为什么你装了迅雷之后电脑会感觉很卡很卡?

    原文地址:http://www.whosmall.com/post/90 关闭极速版迅雷ThunderPlatform.exe进程 ThunderPlatform.exe目的:利用P2P技术进行用户间 ...

  8. 我的SqlHelper类!

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. Win7---------专区

    待完善中-------------------------------------- 1.0Win7来历: 不算上最早的Windows版本从 95 98 2000  ME  XP  2003  Vis ...

  10. 三、Shell变量类型和运算符

    一.Shell变量的应用 1.Shell变量的种类     ·用户自定义变量:由用户自己定义.修改和使用     ·预定义变量:Bash预定义的特殊变量,不能直接修改     ·位置变量:通过命令行给 ...