一.準備資料
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格式:
複製代碼
- openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
|
複製代碼
- openssl pkcs12 -nocerts -out key.pem -in key.p12
|
5)如果需要對key不進行加密:
複製代碼
- openssl rsa -in key.pem -out key.unencrypted.pem
|
6)然後就可以合并兩個.pem檔案, 這個ck.pem就是服務端需要的認證了。
複製代碼
- 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描述檔案裡面的認證要有效。