自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone應(yīng)用程序之APNS推送通知的流程

移動(dòng)開(kāi)發(fā) iOS
本文介紹的是iPhone應(yīng)用程序之APNS推送通知的流程,主要介紹了APNS推送通知的實(shí)現(xiàn),先來(lái)看內(nèi)容。

iPhone應(yīng)用程序之APNS推送通知的流程是本文要介紹的內(nèi)容,主要介紹了APNS推送通知的實(shí)現(xiàn),本文以代碼實(shí)現(xiàn)內(nèi)容的介紹。來(lái)看詳細(xì)內(nèi)容。

下面是我的所有部署配置過(guò)程。

1. 將app注冊(cè)notification里面, 并從APNS上獲取測(cè)試機(jī)的deviceToken.   

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {          
  2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];    
  3.         // other codes here.      
  4.     return YES;  
  5. }  
  6.  
  7. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  
  8.     NSLog(@"deviceToken: %@", deviceToken);  
  9. }  
  10.  
  11. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {  
  12.     NSLog(@"Error in registration. Error: %@", error);  
  13. }  
  14.  
  15. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
  16. {  
  17.       
  18.     NSLog(@"收到推送消息 :%@",[[userInfoobjectForKey:@"aps"] objectForKey:@"alert"]);  
  19.     if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {  
  20.         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"   
  21.  message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]           
  22.  delegate:self          
  23.  cancelButtonTitle:@"關(guān)閉"       
  24.  otherButtonTitles:@"更新?tīng)顟B(tài)",nil];  
  25.         [alert show];  
  26.         [alert release];  
  27.     }  

啟動(dòng)程序,將app注冊(cè)到通知項(xiàng)后,在console里面找到打印的deviceToken:

  1. deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b> 

2. 生成app在服務(wù)端需要的許可證

(1)進(jìn)入Provisioning Portal, 下載Certificates在development下的證書(shū)。

(2) 找到需要測(cè)試的app id,然后enable它在development下的

  1. Apple Push Notification service: Development Push SSL Certificate 

需要輸入(1)中的簽名證書(shū)才可以生成一個(gè)aps_developer_identity.cer.

(3) 雙擊aps_developer_identity.cer,會(huì)打開(kāi)系統(tǒng)的key chain. 在My certificates下找到Apple Development Push Services。需要為certificate和它之下的private key各自export出一個(gè).p12文件。(會(huì)出現(xiàn)設(shè)置密碼過(guò)程)

(4)需要將上面的2個(gè).p12文件轉(zhuǎn)成.pem格式:

  1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12  
  2.  
  3.  openssl pkcs12 -nocerts -out key.pem -in key.p12 

(5)如果需要對(duì)key不進(jìn)行加密:

  1. openssl rsa -in key.pem -out key.unencrypted.pem 

(6)然后就可以合并兩個(gè).pem文件, 這個(gè)ck.pem就是服務(wù)端需要的證書(shū)了。

  1. cat cert.pem key.unencrypted.pem > ck.pem 

3.服務(wù)端push通知到ANPS. 在cocoachina找到了兩種方法:

(1)php驅(qū)動(dòng)。需要將ck.pem和php腳本放到server上。全部的php代碼是:

  1.  <?php 
  2. $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';  
  3. $pass = '123456';   // Passphrase for the private key (ck.pem file)  
  4.  
  5. // Get the parameters from http get or from command line  
  6. $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';  
  7. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];  
  8. $sound = $_GET['sound'] or $sound = $argv[3];  
  9.  
  10. // Construct the notification payload  
  11. $body = array();  
  12. $body['aps'] = array('alert' => $message);  
  13. if ($badge)  
  14.   $body['aps']['badge'] = $badge;  
  15. if ($sound)  
  16.   $body['aps']['sound'] = $sound;  
  17.  
  18. /* End of Configurable Items */  
  19. $ctx = stream_context_create();  
  20. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');    
  21. // assume the private key passphase was removed.  
  22. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
  23.  
  24. // connect to apns  
  25. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  26. if (!$fp) {  
  27.     print "Failed to connect $err $errstr\n";  
  28.     return;  
  29. }  
  30. else {  
  31.    print "Connection OK\n<br/>";  
  32. }  
  33.  
  34. // send message  
  35. $payload = json_encode($body);  
  36. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
  37. print "Sending message :" . $payload . "\n";    
  38. fwrite($fp, $msg);  
  39. fclose($fp);  
  40. ?> 

請(qǐng)求一次

  1. http://127.0.0.1/apns/apns.php?message=A%20test%20message%20from%20localhost&badge=2&sound=received5.caf 

就會(huì)向APNS進(jìn)行一次推送。我的請(qǐng)求結(jié)果如下:

復(fù)制代碼 Connection OK

  1. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}} 

 (2)pushMeBaby驅(qū)動(dòng)。將aps_developer_identity.cer導(dǎo)入到project里面,改名為apns.cer。

啟動(dòng)app即可看到push窗口。pushMeBaby和具體的配置的過(guò)程也基本是參考這個(gè)帖子的,非常感謝cocoachina會(huì)員的奉獻(xiàn)精神。

小結(jié):iPhone應(yīng)用程序之APNS推送通知的流程的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-18 16:42:07

iPhone應(yīng)用APNS推送

2011-08-05 13:49:53

iPhone 應(yīng)用 開(kāi)發(fā)

2011-07-21 10:47:37

iPhone Cocoa 委托

2011-08-05 14:58:58

iPhone CoreAnimat 動(dòng)畫(huà)

2011-08-10 17:30:50

iphoneThree20

2011-08-10 09:31:33

開(kāi)發(fā)iPhone應(yīng)用程

2012-05-24 15:49:35

HTML5

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-07-20 15:58:58

iPhone 應(yīng)用程序 生命周期

2010-08-27 10:41:41

iPhone核心應(yīng)用程序

2011-07-19 14:36:32

iPhone

2011-07-27 17:30:40

iPhone Locate 定位

2011-08-12 14:54:45

iPhone委托

2011-07-21 15:56:32

iPhone 截屏

2011-07-26 11:13:15

iPhone PXL

2024-03-12 10:05:04

應(yīng)用程序推送通知

2011-08-17 16:16:29

iPhone應(yīng)用程序啟動(dòng)過(guò)程

2012-04-26 13:48:56

iPhone應(yīng)用發(fā)布Ad Hoc

2011-07-28 13:59:40

iPhone App

2011-07-26 16:33:56

iPhone Delegate
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)