天天看點

為iPhone 應用搭建php 版push 伺服器的流程 (轉)

為了以後自己研究,收藏網絡大大的push伺服器搭建 文檔。 

在應用裡加入 Push 功能對于使用者及時擷取資訊是非常有幫助的,以前介紹過iPhone 的 Push (推送通知)功能原理淺析,裡面提到要為自己的 App 添加推送功能,開發者先要搭建一個推送伺服器。下面就介紹一個為 iPhone 應用搭建 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.  

   4. $body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'received5.caf'));

   5.  

   6. $ctx = stream_context_create();

   7. stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");

   8.  

   9. $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

  10. if (!$fp) {

  11.     print "Failed to connect $err $errstrn";

  12.     return;

  13. }

  14. print "Connection OK\n";

  15. $payload = json_encode($body);

  16. $msg = chr(0) . pack("n",32) . pack("H*", $deviceToken) . pack("n",strlen($payload)) . $payload;

  17. print "sending message :" . $payload . "\n";

  18. fwrite($fp, $msg);

  19. fclose($fp);

  20. ?>

轉載于:https://www.cnblogs.com/autumnyuhe/archive/2011/11/07/2239613.html

繼續閱讀