iPhone應用程式之APNS推播通知的流程

來源:互聯網
上載者:User

iPhone應用程式之APNS推播通知的流程是本文要介紹的內容,主要介紹了APNS推播通知的實現,本文以代碼實現內容的介紹。來看詳細內容。

下面是我的所有部署配置過程。

1. 將app註冊notification裡面, 並從APNS上擷取測試機的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:@"關閉"       
  24.  otherButtonTitles:@"更新狀態",nil];  
  25.         [alert show];  
  26.         [alert release];  
  27.     }  

啟動程式,將app註冊到通知項後,在console裡面找到列印的deviceToken:

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

2. 產生app在服務端需要的許可證

1)進入Provisioning Portal, 下載Certificates在development下的認證。

2) 找到需要測試的app id,然後enable它在development下的

 
  1. 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格式:

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

5)如果需要對key不進行加密:

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

6)然後就可以合并兩個.pem檔案, 這個ck.pem就是服務端需要的認證了。

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

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

1)php驅動。需要將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. ?> 

請求一次

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

就會向APNS進行一次推送。我的請求結果如下:

複製代碼 Connection OK

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

2)pushMeBaby驅動。將aps_developer_identity.cer匯入到project裡面,改名為apns.cer。

啟動app即可看到push視窗。pushMeBaby和具體的配置的過程也基本是參考這個文章的,非常感謝cocoachina會員的奉獻精神。

小結:iPhone應用程式之APNS推播通知的流程的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.