初學iPhone 開發 ,經過反複多次驗證,結合下面2個教程:
http://ameyashetti.wordpress.com/2009/07/31/apple-push-notification-service-tutorial/
http://www.macoscoders.com/2009/05/17/iphone-apple-push-notification-service-apns/
得出從零開始的php版push伺服器搭建流程:
==============================================================
0.在Mac OS X機器上安裝好XCode , 連接配接一台正常的iPhone, 保持平和的心态
APP 開發基礎設定
1.在iPhone Provisioning Portal中建立好APP ID和Device.
2. 在Keychain Access.app中生成證書請求CertificateSigningRequest.certSigningRequest(菜單 > Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority...).
3.在iPhone Provisioning Portal > Certificates中請求一個證書(點選Request Certificate,上傳CertificateSigningRequest.certSigningRequest).
4.請求完成後,将證書檔案 (developer_identity.cer)下載下傳,輕按兩下導入到Key Chain中.
5.在iPhone Provisioning Portal > Provisioning 中,建立一個Profile, 選擇指定的APP ID和 Devices後生成.
6.将剛剛生成的Profile下載下傳為*_profile.mobileprovision, 輕按兩下該檔案, 将profile加載到iPhone中.
Push Notification service設定
7.在iPhone Provisioning Portal > App IDs,選擇需要Push服務的App ID, 進入Configure.
8.确認 Enable for Apple Push Notification service ,配置 Development Push SSL Certificate, 上傳 第2步生成的證書請求.
9.下載下傳生成的aps_developer_identity.cer, 完成Push服務配置.
10.輕按兩下aps_developer_identity.cer,儲存到Key Chain.
生成php Push Notification sender需要的證書檔案
11.在Keychain Access.app裡標明這個新證書(Apple Development Push Services*),導出到桌面,儲存為Certificates.p12.
12.運作如下指令:
複制代碼 - openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12
- openssl pkcs12 -nocerts -out key.pem -in Certificates.p12
- openssl rsa -in key.pem -out key.unencrypted.pem
- cat cert.pem key.unencrypted.pem > ck.pem
|
獲得php Push Notification sender所需的裝置令牌:
13.建立一個View-based Application項目,在$PROJECT_NAMEAppDelegate.m中:
a.粘貼如下代碼 :
複制代碼 - - (void)applicationDidFinishLaunching:(UIApplication *)app {
- // other setup tasks here….
- [window addSubview:viewController.view];
- [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
- }
- - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- //NSLog(@"devToken=%@",deviceToken);
- [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
- }
- - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
- NSLog(@"Error in registration. Error: %@", err);
- [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
- }
- -(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle{
- UIAlertView *alert;
- if([otherTitle isEqualToString:@""])
- alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
- else
- alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
- [alert show];
- [alert release];
- }
|
b.在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 方法中增加
複制代碼 - [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
|
14.項目設定
a.Targets > $APP_NAME > context menu > Properties > Identifier
修改 identifier 為App ID
b.Targets > $APP_NAME > context menu > Build > Code Signing > Code Signing Identifier > Any iPhone OS Device
指定 iPhone Developer 為開發用機
15.編譯并運作後會在iPhone上顯示裝置令牌
16.php Push Notification sender代碼如下:
複制代碼 - <?php
- $deviceToken = "裝置令牌";
- $body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'received5.caf'));
- $ctx = stream_context_create();
- stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");
- $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;
- }
- print "Connection OK/n";
- $payload = json_encode($body);
- $msg = chr(0) . pack("n",32) . pack("H*", $deviceToken) . pack("n",strlen($payload)) . $payload;
- print "sending message :" . $payload . "/n";
- fwrite($fp, $msg);
- fclose($fp);
- ?>
|
==============================================================
希望各位高手能指正這流程中遺漏或不清楚的地方.
第一個問題:
我知道 Push 服務實際上是向APNS發送一個附帶證書的ssl請求, 由APNS來通知iPhone.
上面例子中,php發送請求時将裝置令牌和證書一并發送給APNS,我想知道有沒有辦法能更輕松的得到裝置令牌.
答案:裝置令牌 由你的app部署到用戶端後 上傳到伺服器獲得.
第二個問題:
如果我的APP釋出之後,要在 iPhone Provisioning Portal 中啟用 Production Push SSL Certificate(開發時用的是Development Push SSL Certificate),
這兩種證書狀态時的令牌是一樣的麼.
如果我同時啟用了兩個證書, php給APNS發請求時,APNS會接受哪一個呢?
答案:這2個證書 對的是2個apns 伺服器 開發的是開發的 正式的是正式的. 2個鎖2個鑰匙.不會同時有效
本文轉載自http://www.cocoachina.com/bbs/read.php?tid-30410-fpage-2.html