標籤:style http java os io 檔案 for ar
別的不說,現在AppDelegate.m中添加以下代碼塊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // 將這行代碼插入 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];}
- (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{ NSDictionary *dic = [userInfo objectForKey:@"aps"]; NSLog(@"收到的推送訊息%@", [dic objectForKey:@"alert"]); if (nil != [dic objectForKey:@"alert"]) { UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"推播通知" message:[dic objectForKey:@"alert"] delegate:self cancelButtonTitle:@"關閉" otherButtonTitles:@"更新狀態", nil]; [alterView show]; [alterView release]; }}
將列印出來的deviceToken 去掉空格和<>符號,產生一個許可證號留著
======================================================
產生測試認證:
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
================================================================
用php寫伺服器 send.php
#!/usr/bin/php<?php$deviceToken = ‘3cec682c9b773f2d82e337a98c35483aaa4f4c74e3933f9cb57e9d4c2f17f52d‘; // masked for security reason// Passphrase for the private key (ck.pem file)$pass = ‘‘;// Get the parameters from http get or from command line$message = $_GET[‘message‘] or $message = $argv[1] or $message = ‘Message received from javacom‘;$badge = (int)$_GET[‘badge‘] or $badge = (int)$argv[2];$sound = $_GET[‘sound‘] or $sound = $argv[3];$message = ‘Hello qwe‘;$badge = 1;$sound = ‘cowbell.wav‘;// 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);$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 OK\n";}$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);?>
把php檔案與ck.pem放到同一檔案夾下。
在命令列中,php send.php 即可測試