IOS推送實現和服務端測試+注意事項

來源:互聯網
上載者:User

一.準備資料

1.在app認證建立推送認證

(下載並安裝...記住保留p12(cer.p12和key.p12)+cer+mobileprovision以後更新包用到,避免撤銷後服務端要重新設定新的p12)

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

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

複製代碼

  1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12

複製代碼

  1. 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

二.代碼實現:

1.IOS代碼:

1.代理

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//.....//**** 訊息推送 ****[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
//.....
return YES;}

#pragma mark -#pragma mark push delegate- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{NSString *deviceTokenString=[NSString stringWithFormat:@"%@",deviceToken];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@"<" withString:@""];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@">" withString:@""];deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//[UIApplication sharedApplication].applicationIconBadgeNumber=0;}

2.服務端測試php代碼:

<?php$deviceToken = '裝置號.....';$pass = '......';//ck.pem密碼//定義訊息和聲音$message = $_GET['message'] or $message = $argv[1] or $message = 'message by kllmctrl'; $badge = (int)$_GET['badge'] or $badge = 3;//數量$sound = $_GET['sound'] or $sound = 'default';//聲音//要再用戶端顯示的對話方塊$body = array();$body['aps'] = array('alert' => $message);if ($badge)  $body['aps']['badge'] = $badge;if ($sound)  $body['aps']['sound'] = $sound;//檔案路徑$ctx = stream_context_create();stream_context_set_option($ctx, 'ssl', 'local_cert', './ck.pem');//ck.pem所在的路徑//配置路徑stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);//連結apple$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/>";}//發送訊息$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://localhost:8888/push.php

注意:iphone描述檔案裡面的認證要有效。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.