iphone 搭建php版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.运行如下命令:
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 为开发用机,编译并运行后会在iphone上显示设备令牌
php push notification sender代码如下:
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);
?>
iphone 搭建php版push服务器 实例操作的内容介绍完
1 楼 cjsen 2011-12-15 请问iphone push推送中,如果我想将同一条信息发给很多个设备,有没有接口或方法说:我从服务器“将要发的所有设备id和一条信息”发到apns上,苹果服务器自动帮我为每一个设备发信息,而不用我自己在服务器上“一个设备id加一条信息”一个个的发;
