Apple PUSH Notication Service (APNS) 配置攻略
2011-07-19 11:08
323 查看
iOs 3.0以后就支持APNS( apple push notication Service).下面介绍怎么配置APNS服务。
APNS 分为客户端与服务器端2个部分:
客户端部分:
1 创建一个App Id 。如果你已经是apple的注册用户(至少是开发者)。首先登录进入Apple developer program portal
2 创建好App Id 点击 Configure。选择支持APNS,按照步骤选择你的开发证书(csr)。并下载cer文件双击安装( Apple development iOS push notication Services:xxxxxxxxx)
3 此时 。Mac 上的 keychain 里面 Login 已经包含了想要的证书和密钥,按Control 将 证书和密钥export出。保存为cert.p12 和 key.p12 文件(导出过程需要输入密码。请务必保留这个密码)
4 打开终端。 cd 到保护cert.p12 key.p12的文件夹。将cert.p12 he key.p12转化为pem文件,并进行合并
openssl pkcs12 -clcerts -nodes -out cert.pem -in cert.p12
openssl pkcs12 -nodes -out key.pem -in key.p12
cat cert.pem key.pem > yourapp.pem
5 生产对应App Id 的 privosion file。并加入XCode。用这个证书对你的应用进行签名(注意,App ID 必须匹配)
6 APNS 客户端主要涉及下面几个API
6.1 将app注册notification里面.
6.2 从APNS上获取测试机的deviceToken.
3 当收到 PUSH 的时候,处理
客户端部分基本上完成了,需要保留好 pem文件,p12文件的密码,和获得的DeviceToke,服务器方面需要使用到
服务器方面:
1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
请 求一次 http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
将php部署好,在IE中访问即可。
经过测试,可以在真机上获得Push消息。
在iPhone4.3已测试通过。
APNS 分为客户端与服务器端2个部分:
客户端部分:
1 创建一个App Id 。如果你已经是apple的注册用户(至少是开发者)。首先登录进入Apple developer program portal
2 创建好App Id 点击 Configure。选择支持APNS,按照步骤选择你的开发证书(csr)。并下载cer文件双击安装( Apple development iOS push notication Services:xxxxxxxxx)
3 此时 。Mac 上的 keychain 里面 Login 已经包含了想要的证书和密钥,按Control 将 证书和密钥export出。保存为cert.p12 和 key.p12 文件(导出过程需要输入密码。请务必保留这个密码)
4 打开终端。 cd 到保护cert.p12 key.p12的文件夹。将cert.p12 he key.p12转化为pem文件,并进行合并
openssl pkcs12 -clcerts -nodes -out cert.pem -in cert.p12
openssl pkcs12 -nodes -out key.pem -in key.p12
cat cert.pem key.pem > yourapp.pem
5 生产对应App Id 的 privosion file。并加入XCode。用这个证书对你的应用进行签名(注意,App ID 必须匹配)
6 APNS 客户端主要涉及下面几个API
6.1 将app注册notification里面.
- (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]; } |
服务器方面:
1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
<?php $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b'; // 可以用你获得的DeviceToken替换 $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); ?> |
Connection OK Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}} |
经过测试,可以在真机上获得Push消息。
在iPhone4.3已测试通过。
相关文章推荐
- Apple PUSH Notication Service (APNS) 配置攻略
- Apple Push Notification Service(APNS)开发指引
- iPhone Apple Push Notification Service (APNS)(全教程)
- iOS APNS 訊息推播 – Apple Push Notification Service 介紹
- Apple Push Notification Service (APNS)原理与实现方案
- Apple Push Notification Service (APNS)原理与实现方案
- APNS(Apple Push Notification Service)远程推送原理解析
- Apple Push Notification Service(APNS)相关
- iPhone Apple Push Notification Service (APNS)(全教程)
- [转]宝文!Apple Push Notification Service (APNS)原理与实现方案
- iPhone Apple Push Notification Service (APNS)(全教程)
- iPhone Apple Push Notification Service (APNS)(全教程)
- APNS - Apple Push Notification Service
- java libs for apns:apple push notification service
- iPhone Apple Push Notification Service (APNS)
- iPhone Apple Push Notification Service (APNS)
- APNs(英文全称:Apple Push Notification service)Demo与实践
- iphone 推送服务--Apple Push Notification Service
- 苹果信息推送服务(Apple Push Notification Service)使用总结
- 苹果信息推送服务(Apple Push Notification Service)使用总结