IOS客户端的APNS服务简介与实现
2013-03-05 20:10
639 查看
APNs --Apple Push Notification Service
APNs 苹果推送服务器
Device 安装带有推送服务程序的iPhone手机
Provider 程序服务器,把需要推送的信息发给 APNs
DeviceToken 在Device第一次连接APNs时,由APNs生成的经过加密的连接认证信息。在以后的连接中,无论时Provider到APNs还是APNs到Device 都需要 DeviceToken作为认证。
Payload 需要推送的消息的主体内容。alert-alert消息的消息体,按键标题等badge-显示在程序icon右上角的数字,sound-声音提示文件的文件名,该声音资源文件要在程序包中。
整体流程大体分为五个步骤:
1: Device --> 连接--> APNs 获取 DeviceToken
2: Device-->连接-->Provider提供DeviceToken
3: Provider侦测需要push的消息生成Notification信息
4: Provider侦把要push的消息推送到APNs
5: APNs把该消息推送到手机
介绍完APNS的概况,下面再了解一下具体的实现方法:
注:先申请APNS的证书,再进行以下操作。
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// other codes here.
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken: %@", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error in registration. Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@" 关闭"
otherButtonTitles:@" 更新状态",nil];
[alert show];
[alert release];
}
}
启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
2. 生成app在服务端需要的许可证
1)进入Provisioning Portal, 下载Certificates在development下的证书。
2) 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3) 双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key各自export出一个.p12文件。(会出现设置密码过程)
4)需要将上面的2个.p12文件转成.pem格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
5)如果需要对 key不进行加密:
openssl rsa -in key.pem -out key.unencrypted.pem
6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
cat cert.pem key.unencrypted.pem > ck.pem
3. 服务端push通知到ANPS. 在cocoachina找到了两种方法:
1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
<?php
$deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
$pass = '123456'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$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
$payload = json_encode($body);
$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);
?>
请 求一次 http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
Connection OK
Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}
参考文章
http://blog.sina.com.cn/s/blog_46e3e7db0100roqt.html http://www.cocoachina.com/iphonedev/sdk/2010/0610/1668.html
(图片来源--apple 开发文档)
APNs 苹果推送服务器
Device 安装带有推送服务程序的iPhone手机
Provider 程序服务器,把需要推送的信息发给 APNs
DeviceToken 在Device第一次连接APNs时,由APNs生成的经过加密的连接认证信息。在以后的连接中,无论时Provider到APNs还是APNs到Device 都需要 DeviceToken作为认证。
Payload 需要推送的消息的主体内容。alert-alert消息的消息体,按键标题等badge-显示在程序icon右上角的数字,sound-声音提示文件的文件名,该声音资源文件要在程序包中。
整体流程大体分为五个步骤:
1: Device --> 连接--> APNs 获取 DeviceToken
2: Device-->连接-->Provider提供DeviceToken
3: Provider侦测需要push的消息生成Notification信息
4: Provider侦把要push的消息推送到APNs
5: APNs把该消息推送到手机
介绍完APNS的概况,下面再了解一下具体的实现方法:
注:先申请APNS的证书,再进行以下操作。
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// other codes here.
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken: %@", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error in registration. Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@" 关闭"
otherButtonTitles:@" 更新状态",nil];
[alert show];
[alert release];
}
}
启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
2. 生成app在服务端需要的许可证
1)进入Provisioning Portal, 下载Certificates在development下的证书。
2) 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3) 双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key各自export出一个.p12文件。(会出现设置密码过程)
4)需要将上面的2个.p12文件转成.pem格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
5)如果需要对 key不进行加密:
openssl rsa -in key.pem -out key.unencrypted.pem
6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
cat cert.pem key.unencrypted.pem > ck.pem
3. 服务端push通知到ANPS. 在cocoachina找到了两种方法:
1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
<?php
$deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
$pass = '123456'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$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
$payload = json_encode($body);
$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);
?>
请 求一次 http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
Connection OK
Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}
参考文章
http://blog.sina.com.cn/s/blog_46e3e7db0100roqt.html http://www.cocoachina.com/iphonedev/sdk/2010/0610/1668.html
(图片来源--apple 开发文档)
相关文章推荐
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS客户端的APNS服务简介与实现
- iOS之推送通知-本地-服务器3.服务器端实现: 如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceTo
- ZPush--基于netty4实现的苹果通知推送服务(APNs)Java客户端
- 搭建基于asp.net的wcf服务,ios客户端调用的实现记录
- iOS APNS 客户端实现流程与代码(附详细说明)
- NodeJs 实现IOS APNS 消息推送服务
- ios开发笔记之 APNS推送服务的实现
- 用于实现Web服务的SOA编程模型,第1部分:IBM SOA编程模型简介
- iOS网络编程实践--NSStream实现TCP Socket iPhone客户端
- iOS远程推送通知服务APNs
- iOS APNs 实现多语言及自定义事件
- iOS —— 客户端Socket UDP协议广播机制的实现
- (三)Java EE 5实现Web服务(Web Services)及多种客户端实例-瘦客户端
- 服务端使用Axis2-1.6.3发布webservice服务、客户端使用Axis1.4实现调用