首先,需要一个pem的证书,该证书需要与开发时签名用的一致。 具体生成pem证书方法如下:

1. 登录到 iPhone Developer Connection Portal(http://developer.apple.com/iphone/manage/overview/index.action ) 并点击 App IDs

2. 选择对应App ID。(开发与发布不一样的。需注意)

3. 点击App ID旁的“Configure”,然后按下按钮生产 推送通知许可证。根据“向导” 的步骤生成一个签名并上传,最后下载生成的许可证。

4. 通过双击.cer文件将你的 aps_developer_identity.cer 引入Keychain中。

6. 单机“Apple Development Push Services”,导出p12文件,保存为 apns-dev-cert.p12 文件。

7. 扩展“Apple Development Push Services” 对“Private Key”做同样操作,保存为 apns-dev-key.p12 文件。

8. 需要通过终端命令将这些文件转换为PEM格式:

openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12

openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12

9. 如果你想要移除密码,要么在导出/转换时不要设定或者执行:

openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem

10. 最后,你需要将键和许可文件合成为apns-dev.pem文件,此文件在连接到APNS时需要使用:

cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem

PHP后台写法:

<?php

    // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)

//deviceToken  在测试版本和上线版本上不一样。    

    //lei ipod touch
$deviceToken = '06fe0a85056f6b9f07fb11a4eed962aaab824b9522660a8bb165b369717159ab'; // Put your private key's passphrase here:
$passphrase = 'abc123456'; // Put your alert message here:
$message = 'My first push test!'; //////////////////////////////////////////////////////////////////////////////// $message = array('msg'=>'小小说阅读器','title'=>'小小说','url'=>'http://www.apple.com.cn');
//$message = array('msg'=>'去商品详细页面','itemtype'=>'2','id'=>'192172');
//$message = array('msg'=>'去菜单页面','itemtype'=>'1','zktype'=>'1','order'=>'1','zksubtype'=>'1','zktitle'=>'9.9包邮');
//$message = array('msg'=>'软件升级'); $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dev.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server //这个为正是的发布地址
//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //这个是沙盒测试地址,发布到appstore后记得修改哦
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array(
'alert' => '逗你玩!哈哈。',
'sound' => 'beep.wav',
'badge' => 1
);
$body['type']=2;
$body['data']=$message; // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>

客户端代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch. main = [[MainViewController alloc] init];
navMain = [[UINavigationController alloc] initWithRootViewController:main];
self.window.rootViewController = navMain; notifiDic = [[NSMutableDictionary alloc] init]; [self initNotification]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; NSLog(@"launchOption==%@",[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] != nil) { if (notifiDic.count != 0) {
[notifiDic removeAllObjects];
}
[notifiDic setDictionary:(NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"didFinishLaunching" message:[NSString stringWithFormat:@"didFinishLaunching:\n%@",launchOptions] delegate:self cancelButtonTitle:@"Cancek" otherButtonTitles:@"OK", nil];
[alert show];
alert.tag = 101;
[alert release]; } self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} - (void)initNotification{
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
}
#pragma mark -
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"deviceToken: %@", deviceToken);
NSLog(@"deviceToken===%@",[deviceToken description]); NSRange _range = NSMakeRange(1,[[deviceToken description] length]-2);
NSString *deviceTokenStr = [[deviceToken description] substringWithRange:_range];
NSLog(@"deviceTokenStr==%@",deviceTokenStr);
deviceTokenStr = [deviceTokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"deviceTokenStr==%@",deviceTokenStr); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"product deviceToken" message:deviceTokenStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"%s,,,,%@",__func__,userInfo); if (notifiDic.count != 0) {
[notifiDic removeAllObjects];
}
[notifiDic setDictionary:userInfo]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"受到的通知如下:\n%@",userInfo] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
alert.tag = 100;
[alert release]; } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"%s",__func__);
}

注意点:

1.推送证书都弄好后,项目的开发证书要重新下载一下。

1.反馈服务

Apple 还提供了一个 反 馈服务 ,你应该定期查询。它提供了一个以前使用过但不再有效的(例如用户卸载了你的iPhone程序)设备令牌列表。你可以从你的数据库中删除这些设备令牌。

本教程不涉及反馈服务的使用。

2.创建载荷

使用 PHP 很容易根据数组并 转 换成 JSON而创建载荷:

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');

$payload = json_encode($payload);

显示 $payload 的内容可以看到传送到APNS 的 JSON字符串:

{

"aps" : { "alert" : "This is the alert text", "badge" : 1, "sound" : "default" }

}

这将使消息显示于设备上,触发提升声音并将“1”置于程序图标上。默认按钮“Close”和“View”同时会显示于弹出窗口上。

对于 Server Density iPhone程序而言,让用户按下“View”直接进入产生此提示的服务器是很重要的,所以我们增加了额外的自定义值:

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');

$payload['server'] = array('serverId' => $serverId, 'name' => $name);

$output = json_encode($payload);

当用户按下“View”后,自定义server值将被传递到设备中的程序。JSON 值如下:

{

"aps" : { "alert" : "This is the alert text", "badge" : 1, "sound" : "default" },

"server" : { "serverId" : 1, "name" : "Server name")

}

256字节的限制适用于整个载荷,包括自定义字典集。

  

原生接口

在Server Density中,一旦产生了一条提示,将建立一个载荷并插入队列中。因此有必要时我们可以同时发送多个载荷。

Apple推荐使用这种方法,因为如果你在发送各载荷时频繁连接和断开,APNS有可能会封锁你的IP。

3.Push Notification Provider 是一个应用程序,用于通过 APNs 发送推送通知给 iPhone 应用。

通过 APNs 发送推送通知有几个步骤:
1. 使用前面创建的 SSL 证书与 APNs 通讯;

2. 构造所要发送的消息载体;

3. 发送载体到APNs;

APNs 是一个基于流的 TCP socket,你的 provider 以 SSL 协议与其通讯。推送通知(包括载体)是以二进制流的方式发送的。和APNs 建立连接后,你可以维持该连接并在连接中断之前发送多个通知。

技巧:应避免每发送一次推送通知就建立、关闭一次连接。频繁的建立、关闭连接可能会被 APNs 认为是 DOS 攻击,从而拒绝发送 provider 的推送通知发送请求。

iOS-APNS证书申请与使用的更多相关文章

  1. iOS发布证书申请

    一.  准备工作1.1.准备打包服务器 打包服务器搭建详见http://bbs.justep.com/thread-67724-1-1.html 或 http://www.cnblogs.com/Wo ...

  2. iOS开发者证书申请过程

    真机测试前准备工作:1.苹果的MAC一台.如果你用的是***不知道可不可以,反正我没用过...一般公司都会给你配开发工具的.2.iphone手机一部.(本人纯屌丝,用的iphone4)3.开发者账号. ...

  3. IOS开发者证书申请及应用上线发布详解(2014版)

    其实一直以来我都想做一个最齐全的上传应用到appstore的教程,但一直狠不下心,今天凌晨2点12分,我鼓起勇气写教程,来吧不多说.登录开发者中心:http://developer.apple.com ...

  4. iOS开发者证书申请及应用上线发布详解

    一个小教程登录开发者中心:http://developer.apple.com/ 第零部分:本地生成密钥1.打开mac的钥匙串访问  2.选择钥匙串的证书助理(有些可能是英文的)   3.点击继续后存 ...

  5. Ios生产证书申请(含推送证书)

    一.Mac机上生成请求文件. Mac机上点击证书助手 => 从证书颁发机构请求证书 => 得到CertificateSigningRequest.certSigningRequest请求文 ...

  6. iOS $299刀企业证书申请的过程以及细节补充(二)

    上篇博客写的过程中,没有图,也没有相应的说明.这次再补充一些信息: 1.从 https://developer.apple.com/ios/enroll/dunsLookupForm.action 申 ...

  7. iOS $299刀企业证书申请的过程以及细节补充

    最近申请了iOS的 299刀企业证书,相关过程有些问题,分享出来,以便后来人参考. 申请的过程我主要参考了别人以前的文章,链接如下: 1.https://developer.apple.com/cn/ ...

  8. iOS证书申请详细流程

    一.事前准备 1.1 准备苹果帐号 首先您需要有一个苹果的开发者帐号,一个mac系统.如果没有帐号可以打开申请加入苹果的开发者计划.如何申请网上有详细的介绍,在此不多做介绍. 如果您已经有了一个帐号, ...

  9. iOS 证书申请和使用详解(详细版)

    对于iOS开发者来说,apple开发者账号肯定不会陌生.在开发中我们离不开它.下面我简单的为大家分享一下关于iOS开发中所用的证书相关知识. 第一部分:成员介绍 1.Certification(证书) ...

  10. 申请iOS开发者证书

    来源:http://blog.csdn.net/htttw/article/details/7939405 申请iOS开发者证书 今天我们介绍如何申请iOS开发者证书(99刀): 1. 打开 http ...

随机推荐

  1. SQL常见优化Sql查询性能的方法有哪些?

    常见优化Sql查询性能的方法有哪些? 1.查询条件减少使用函数,避免全表扫描 2.减少不必要的表连接 3.有些数据操作的业务逻辑可以放到应用层进行实现 4.可以使用with as 5.使用“临时表”暂 ...

  2. Unable to convert MySQL date/time value to System.DateTime 错误

    C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. Leetcode Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. 洛谷 P1466 集合 Subset Sums Label:DP

    题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...

  6. 从倒影说起,谈谈 CSS 继承 inherit(转)

    从倒影说起,谈谈 CSS 继承 inherit 给定一张有如下背景图的 div: 制作如下的倒影效果: 方法很多,但是我们当然要寻找最快最便捷的方法,至少得是无论图片怎么变化,div 大小怎么变化,我 ...

  7. [BZOJ1997][HNOI2010] 平面图判定

    Description Input Output     是的..BZOJ样例都没给.     题解(from 出题人): 如果只考虑简单的平面图判定,这个问题是非常不好做的. 但是题目中有一个条件— ...

  8. seajs学习一天后的总结归纳

    公司项目最近需要将js文件迁移到seajs来进行模块化管理,由于我以前主要接触模块化开发是接触的AMD规范的requireJS,没有接触过CMD规范,而且在实际项目中还没有用过类似技术.于是,我非常兴 ...

  9. JavaScript 中string方法

    注意:JavaScript的字符串是不可变的(immutable),String类定义的方法不能改变原来字符串内容,例如String.toUpperCase()这样的方法,返回的是全新的字符串,而不是 ...

  10. 【翻译】Express web应用开发 第一章

    本章节是一个对初学者友好的Express介绍.你将学习到Express的基础知识.核心概念和实现一个Express应用的组成部分.现阶段我们不需要做太多的编码,本章节会让你熟悉和习惯Express,为 ...