iPhone應用中APNS推送通知流程代碼實現案例是本文要介紹的內容,主要是如何來實現APNS的推送通知,具體內容來看本文詳細代碼。php
1. 將app註冊notification裏面, 並從APNS上獲取測試機的deviceToken. json
- - (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:app
- deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
二、生成app在服務端須要的許可證socket
(1)進入Provisioning Portal, 下載Certificates在development下的證書。ide
(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不進行加密:spa
- openssl rsa -in key.pem -out key.unencrypted.pem
(6)而後就能夠 合併兩個.pem文件, 這個ck.pem就是服務端須要的證書了。code
- cat cert.pem key.unencrypted.pem > ck.pem
三、服務端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 $errstrn";
- return;
- }
- else {
- print "Connection OKn<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"}}
(2)pushMeBaby 驅動。將aps_developer_identity.cer導入到project裏面,更名爲apns.cer。
小結:iPhone應用中APNS推送通知流程代碼實現案例的內容介紹完了,但願經過本文的學習能對你有所幫助!