您好,欢迎访问一九零五行业门户网

php如何实现ios推送

php实现ios推送的方法:首先把要发送的消息以及iphone标识打包,并发送给apns;然后查找有相应标识的iphone,并把消息发送到iphone;最后把发来的消息传递给相应的应用程序,并且按照设定弹出push通知即可。
推荐:《php视频教程》
ios推送消息是许多ios应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下:
ios消息推送的工作机制可以简单的用下图来概括:
provider是指某个iphone软件的push服务器,apns是apple push notification service的缩写,是苹果的服务器。
上图可以分为三个阶段:
第一阶段:应用程序把要发送的消息、目的iphone的标识打包,发给apns。 
第二阶段:apns在自身的已注册push服务的iphone列表中,查找有相应标识的iphone,并把消息发送到iphone。 
第三阶段:iphone把发来的消息传递给相应的应用程序,并且按照设定弹出push通知。
从上图我们可以看到:
1、应用程序注册消息推送。
2、ios从apns server获取device token,应用程序接收device token。
3、应用程序将device token发送给push服务端程序。
4、服务端程序向apns服务发送消息。
5、apns服务将消息发送给iphone应用程序。
无论是iphone客户端和apns,还是provider和apns,都需要通过证书进行连接。
下面我介绍一下几种用到的证书。
一、csr文件
1、生成certificate signing request(csr)
2、填写你的邮箱和常用名称,并选择保存到硬盘。
点击继续:
这样就在本地生成了一个push.certsigningrequest文件。
二、p12文件
1、导出密钥。
2、输入你的密码。
这样就生成了一个push.p12文件。
三、ssl certificate文件 
1、用你付过费的帐号登录到ios provisioning portal,并新建一个app id,这个过程可以参考:ios应用的真机调试,这样就会生成下面这条记录:
2、点击右侧的configure:
3、点击development push ssl certificate一行后的configure:
4、点击continue:
5、选择前面生成好的push.certsigningrequest文件,点击generate,出现如下所示的页面:
6、点击continue:
7、点击download,并将文件命名为aps_developer_identity.cer。
 8、点击done,你会发现状态变成了enabled:
到现在为止,我们已经生成了三个文件:
1、push.certsigningrequest
2、push.p12
3、aps_developer_identity.cer
双击aps_developer_dientity.cer 注册到你的钥匙串中,这样你的钥匙串中就会有
二、准备profile证书,因为推送消息只能再真机上测试,所以要建一个profile证书
点击new profile为上面新建的app id建个profile ,成功之后下载*_dev_profile.mobileprovision
双击将其加入到xcode 的provisioning profiles 中,这里有一点要注意,再将这个加入xcode之前如果之前已经加入过一定要把之前加入的删掉,如果有多个的话会出错。
三、工程代码
到这里证书已经准备完毕,接下来,我们在xcode中新建一个测试工程,注意设置工程的bundle identifier必须与上面建的app id 里的相同
在didfinishlaunchingwithoptions 中加入一下代码
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions{ [self.window makekeyandvisible]; [[uiapplication sharedapplication] registerforremotenotificationtypes: uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert]; return yes; } - (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)ptoken { nslog(@"regisger success:%@", ptoken); //注册成功,将devicetoken保存到应用服务器数据库中 } - (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo{ // 处理推送消息 uialertview *alert=[[uialertview alloc]initwithtitle:@"通知" message:@"我的信息" delegate:selfcancelbuttontitle:@"取消" otherbuttontitles:nil, nil]; [alert show]; [alert release];nslog(@"%@", userinfo);}- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error { nslog(@"regist fail%@",error); }
到这里一切顺利的话我们就可以在真机运行了,注册成功我们会得到iphone 的devicetoken,
my token is:
<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>
四、在应用服务器采用php的方式将消息推送给apns,
1、php连接apns也是需要证书的,还记得我们上面获得的几个证书吗?打开终端,对上面的证书做如下处理,
cd 进入证书所在目录
把.cer文件转换成.pem文件:
$ openssl x509 -in aps_developer_identity.cer -inform der-out pushchatcert.pem
把私钥push.p12文件转换成.pem文件:
$ openssl pkcs12 -nocerts -out pushchatkey.pem -in push.p12enter import password:mac verified okenter pem pass phrase:verifying – enter pem pass phrase:
你首先需要为.p12文件输入passphrase密码短语,这样openssl可以读它。然后你需要键入一个新的密码短语来加密pem文件。还是使用”pushchat”来作为pem的密码短语。你需要选择一些更安全的密码短语。
注意:如果你没有键入一个pem passphrase,openssl将不会返回一个错误信息,但是产生的.pem文件里面将不会含有私钥。
最后。把私钥和证书整合到一个.pem文件里:
$ cat pushchatcert.pem pushchatkey.pem > ck.pem
为了测试证书是否工作,执行下面的命令:
$ telnet gateway.sandbox.push.apple.com 2195trying 17.172.232.226…connected to gateway.sandbox.push-apple.com.akadns.net.escape character is ‘^]’.
它将尝试发送一个规则的,不加密的连接到apns服务。如果你看到上面的反馈,那说明你的mac能够到达apns。按下ctrl+c 关闭连接。如果得到一个错误信息,那么你需要确保你的防火墙允许2195端口。
然后再次连接,这次用我们的ssl证书和私钥来设置一个安全的连接:
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195-cert pushchatcert.pem -key pushchatkey.pementer pass phrase for pushchatkey.pem:
你会看到一个完整的输出,让你明白openssl在后台做什么。如果连接是成功的,你可以键入一些字符。当你按下回车后,服务就会断开连接。如果在建立连接时有问题,openssl将会给你一个错误消息
ck.pem文件就是我们需要得到php连接apns 的文件,将ck.pem和push.php放入同一目录上传到服务器,push.php的代码如下:
<?php// 这里是我们上面得到的devicetoken,直接复制过来(记得去掉空格)$devicetoken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';// put your private key's passphrase here:$passphrase = 'abc123456';// put your alert message here:$message = 'my first push test!';$ctx = stream_context_create();stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);// open a connection to the apns server//这个为正是的发布地址 //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //stream_client_connect, $ctx);//这个是沙盒测试地址,发布到appstore后记得修改哦$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, stream_client_connect|stream_client_persistent, $ctx);if (!$fp)exit("failed to connect: $err $errstr" . php_eol);echo 'connected to apns' . php_eol;// create the payload body$body['aps'] = array('alert' => $message,'sound' => 'default');// encode the payload as json$payload = json_encode($body);// build the binary notification$msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload;// send it to the server$result = fwrite($fp, $msg, strlen($msg));if (!$result)echo 'message not delivered' . php_eol;elseecho 'message successfully delivered' . php_eol;// close the connection to the serverfclose($fp);?>
接下来我们访问http://localhost/push/push.php
iphone就会接收到一条推送消息了,如果有问题的话就检查上面的操作步骤,特别是加红的部分
另外去除标记的方法为,在viewdidapper中加入
int badge = [uiapplication sharedapplication].applicationiconbadgenumber; if(badge > 0) { badge--; [uiapplication sharedapplication].applicationiconbadgenumber = badge; }
以上就是php如何实现ios推送的详细内容。
其它类似信息

推荐信息