添加本地推送

///本地添加

  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. 项目分析_xxoo-master

    项目介绍:使用java1.5的原生xml操作类实现 对象<-->xml字符串的相互转化 项目分析:主要分为是三个部分 1.容器类:AbstractContainer         存储x ...

  2. Web Analytics 2.0 中文翻译 [ 系列索引 ]

    引言 内容概述 第一章:网站分析2.0的新奇世界 第二章:选择你的网络分析灵魂伴侣的最佳策略 第三章:点击流分析的奇妙世界:指标 第四章:点击流分析的奇妙世界:实际的解决方案 第五章:荣耀之钥:度量成 ...

  3. Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)

    题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...

  4. RabbitMQ 消息确认机制

    消息确认机制 在之前异常处理部分就已经写了,对于consumer的异常退出导致消息丢失,可以时候consumer的消息确认机制.重复的就不说了,这里说一些不一样的. consumer的消息确认机制 当 ...

  5. lua学习例子

    extern "C"{ #include <lua.h> #include <lauxlib.h> #include <lualib.h> } ...

  6. 面试题目——《CC150》栈与队列

    面试题3.1:描述如何只用一个数组来实现三个栈. 方法1:固定分割 方法2:弹性分割(较难) package cc150; public class ArrayStack { public stati ...

  7. OPenCL

    OpenCLhttp://baike.baidu.com/link?url=7uHWCVUYB3Sau_xh3OOKP-A08_IvmT1SJixdAXKezCuCfkzeSQDiSmesGyVGk8 ...

  8. 关于学习angularJS 的 心里路程(二)

    这一次主要的学习内容是 ng-route(本次的项目由于种种原因吧,我们采用了ui-router,而不是原生的ng-route) * 配置路由. * 注意这里采用的是ui-router这个路由,而不是 ...

  9. Bash 中的 $0 在什么时候不是 argv[0]

    每个 C 程序都有一个 main 函数,每个 main 函数都有一个 argv 参数,这个参数是一个字符串数组,这个数组的值是由该 C 程序的父进程在通过 exec* 函数启动它时指定的. 很多人说 ...

  10. mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)

    mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...