Home > Backend Development > PHP Tutorial > IOS php server client preparation_PHP tutorial

IOS php server client preparation_PHP tutorial

WBOY
Release: 2016-07-13 17:18:39
Original
1061 people have browsed it

今天终于完完全全的搞明白了,APNs 推送前,客户端的准备工作需要那一些。如果有错误的地方,欢迎大神指出来。
Copy after login

Preparation:

Original address: Click to open the link http://blog.csdn.net/tuxiangqi/article/details/17245553

1. Go to the developer center, create an app ID, and select the push notification function (game center and the other one are required).



2.Create certificates

Note: For internal development, you can only use Development. Production is done after testing and when you are ready to publish it to the app store.

According to the introduction of the Develop process, a CSR file will be created (the production version can also share this CSR file), which is created through the keychain (an application in the general menu). For details, see the official website 9fu1tXJ+ rL6s/a1xM7EvP48YnI+CjwvcD4KPHA+My60tL2oY2VydCDOxLz+oaO4+b7dtdq2/rK9o6y74bXDtb3Su7j2Y2VydM7EvP6jrGNlcnTOxLz+t9YgZGV2us1wcm+w5rG+oaPXotLiz tLDx9PDZGV2suLK1KOscHJvyse3orK8PC9wPgo8cD40LrW8yOtjZXJ0IM7EvP7I6yDUv7PXtK48L3A+C jxwPjxpbWcgc3JjPQ=="http://www.Bkjia.com/uploadfile/Collfiles/20131211/201312110 9360487.jpg" alt="IOS php server client preparation_PHP tutorial" target="_blank" class="keylink">

5. Make PEM format files of SSL and private key (required by php server)

a. Create p12 format files of SSL (cert file) and key (private key) respectively, and obtain them through export. (note the naming)


b. Enter the command through the terminal shell to get two PEM format files of SSL and key, and combine them into one



The command parameters are as follows:


Original address: Click to open the link http://blog.csdn.net/tuxiangqi/article/details/17245553

cd path to save the folder

openssl pkcs12 -clcerts -nokeys -out output file name.pem -in original file name such as SSL (Cert file).p12 (change the SSLcert file from p12 to pem)

openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12 (change the key.p12 file to pem, you will be prompted here to set the decompression password of the private key, at least 4 digits , you can enter it)


openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem (This command is to remove at least the 4-digit password just set in the private key. If you want to keep it, you can, but it needs to be Explain to colleagues on the php server)


cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem (This command compiles SSL and private key into a file pem. You can change the name yourself)

The following are other resources found on the Internet, about php server settings


<?php          
$_POST['token'] = "fe28006a9d57b0727514cf42e9549446f0d4fc509cdexxxxxxxxxx";
	    $deviceToken = $_POST['token']; //取得设备的Token,获取方法便见下文	   
	    $body = array("aps" => array("alert" => "message123gggg32323333", "badge" => 1, "sound"=>'default'));  //推送方式,包括了提示内容,提示方式和提示声音。
	    
	    $ctx = stream_context_create();
	    
	    //如果在Windows的服务器上,寻找pem路径会有问题,路径修改成这样的方法:
	    $pem = !empty($this->cfg['isga']) && $this->cfg['isga'] == 2?'apns-dev.pem':'apns-dev.pem'; //临时全部为开发状态
	    stream_context_set_option($ctx, 'ssl', 'local_cert', "/data/web/cert/".$pem);
	    //linux 的服务器直接写pem的路径即可
	    
	    stream_context_set_option($ctx, 'ssl', 'local_cert', <pre name="code" class="html">"/data/web/cert/".$pem);
<pre name="code" class="html"><pre name="code" class="html">	    //如果你的pem存有密码,需要加一个密码登陆语句	
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
        
        //如果你的pem存有密码,需要加一个密码登陆语句        
        //$pass = ”123123“;
        //stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $pass);
        
        //此处有两个服务器需要选择,如果是开发测试用,选择第二名sandbox的服务器并使用Dev的pem证书,如果是正是发布,使用Product的pem并选用正式的服务器        
        $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        
        $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        
        if (!$fp) {        
            print_r("Failed to connect $err $errstrn");        
            return;        
        }        
        print_r("Connection OK\n");        
        $payload = json_encode($body);        
        $msg = chr(0).pack("n", 32).pack('H*', str_replace(' ', '', $deviceToken)).pack("n",strlen($payload)).$payload;               
        print_r("sending message :".$payload."\n");        
        fwrite($fp, $msg);        
        fclose($fp);

?>
Copy after login
It's almost done here, you can follow other server tutorials to get it done. . . There are many online.

Of course, yours

As for the interaction with the php server, it ends here


http://www.bkjia.com/PHPjc/621632.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621632.htmlTechArticleToday I finally fully understood what preparations are required on the client before pushing APNs. If there are any mistakes, experts are welcome to point them out. Preparation work: Original address:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template