APNS推送通知的流程
2011-05-05 16:13
344 查看
marshluca,帖子地址 http://www.cocoachina.com/bbs/read.php?tid-20723.html 1.
将app注册notification里面, 并从APNS上获取测试机的deviceToken. -
(BOOL)application:(UIApplication *)application did
本文作者为CocoaChina会员“marshluca”,帖子地址 http://www.cocoachina.com/bbs/read.php?tid-20723.html
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]; } } |
deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b> |
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 |
openssl rsa -in key.pem -out key.unencrypted.pem |
cat cert.pem key.unencrypted.pem > ck.pem |
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); ?> |
sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
Connection OK Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}} |
通过定时器将数据推送给表格,实现数据刷新的
以下代码由 CocoaChina 会员“zhangnan522”分享,主要实现数据定时推送,在表格里自动更新的功能。
附件下载 reloadTable.zip
(584 K)
相关文章推荐
- APNS推送通知的流程
- APNS推送通知的流程
- iPhone应用中APNS推送通知流程代码实现案例
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- iPhone应用中APNS推送通知流程代码实现案例
- iPhone应用中APNS推送通知流程代码实现案例
- APNS推送通知的流程
- iPhone应用中APNS推送通知流程代码实现案例
- APNS推送通知的流程
- APNS推送通知的流程
- iPhone开发之 - 苹果推送通知服务(APNs)编程
- 无法使用 Apple 推送通知服务(APNs)
- 苹果通知推送服务(APNS)一些关键特性摘要