分享一個搭建php版push伺服器的流程

來源:互聯網
上載者:User

轉自:http://www.cocoachina.com/bbs/read.php?tid=30410

 

初學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.運行如下命令:

複製代碼

  1.     openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12
  2.     openssl pkcs12 -nocerts -out key.pem -in Certificates.p12
  3.     openssl rsa -in key.pem -out key.unencrypted.pem
  4.     cat cert.pem key.unencrypted.pem > ck.pem


獲得php Push Notification sender所需的裝置令牌:
13.建立一個View-based Application項目,在$PROJECT_NAMEAppDelegate.m中:
a.粘貼如下代碼:

複製代碼

  1. - (void)applicationDidFinishLaunching:(UIApplication *)app {
  2.     // other setup tasks here….
  3.     [window addSubview:viewController.view];
  4.     [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  5.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
  6. }
  7. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  8.     //NSLog(@"devToken=%@",deviceToken);
  9.     [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  10. }
  11. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
  12.     NSLog(@"Error in registration. Error: %@", err);
  13.     [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  14. }
  15. -(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle{
  16.     UIAlertView *alert;
  17.     if([otherTitle isEqualToString:@""])
  18.         alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
  19.     else
  20.         alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
  21.     [alert show];
  22.     [alert release];
  23. }


b.在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 方法中增加

複製代碼

  1.     [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  2.     [[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代碼如下:

複製代碼

  1. <?php
  2. $deviceToken = "裝置令牌";
  3. $body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'received5.caf'));
  4. $ctx = stream_context_create();
  5. stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");
  6. $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  7. if (!$fp) {
  8.     print "Failed to connect $err $errstrn";
  9.     return;
  10. }
  11. print "Connection OK\n";
  12. $payload = json_encode($body);
  13. $msg = chr(0) . pack("n",32) . pack("H*", $deviceToken) . pack("n",strlen($payload)) . $payload;
  14. print "sending message :" . $payload . "\n";
  15. fwrite($fp, $msg);
  16. fclose($fp);
  17. ?>


==============================================================

相關文章

聯繫我們

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