iPhone 搭建PHP版Push服務(wù)器 實例操作
iPhone 搭建PHP版Push服務(wù)器 實例操作是本文介紹的內(nèi)容。在應(yīng)用里加入 Push 功能對于用戶及時獲取信息是非常有幫助的,以前介紹過 iPhone 的 Push (推送通知)功能原理淺析,里面提到要為自己的 App 添加推送功能,開發(fā)者先要搭建一個推送服務(wù)器。下面就介紹一個為 iPhone 應(yīng)用搭建 php 版 push 服務(wù)器的流程。
0.在Mac OS X機器上安裝好XCode, 連接一臺正常的iPhone, 保持平和的心態(tài)
APP 開發(fā)基礎(chǔ)設(shè)置
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)下載,雙擊導(dǎo)入到Key Chain中.
5.在iPhone Provisioning Portal > Provisioning 中,新建一個Profile, 選擇指定的APP ID和 Devices后生成.
6.將剛剛生成的Profile下載為*_profile.mobileprovision, 雙擊該文件, 將profile加載到iPhone中.
Push Notification service設(shè)置
7.在iPhone Provisioning Portal > App IDs,選擇需要Push服務(wù)的App ID, 進(jìn)入Configure.
8.確認(rèn) Enable for Apple Push Notification service ,配置 Development Push SSL Certificate, 上傳第2步生成的證書請求.
9.下載生成的aps_developer_identity.cer, 完成Push服務(wù)配置.
10.雙擊aps_developer_identity.cer,保存到Key Chain.
生成php Push Notification sender需要的證書文件
11.在Keychain Access.app里選定這個新證書(Apple Development Push Services*),導(dǎo)出到桌面,保存為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所需的設(shè)備令牌:
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.項目設(shè)置
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 為開發(fā)用機,編譯并運行后會在iPhone上顯示設(shè)備令牌
php Push Notification sender代碼如下:
- <?php
- deviceToken = "設(shè)備令牌";
- $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;
- rint "sending message :" . $payload . "\n";
- fwrite($fp, $msg);
- fclose($fp);
- ?>
小結(jié):iPhone 搭建PHP版Push服務(wù)器 實例操作的內(nèi)容介紹完了,希望本文對你有幫助。