欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

如何在自己的电脑上配置APNS推送环境

程序员文章站 2022-11-08 13:22:28
本文只是记录一下如何在自己的电脑上配置apns推送环境,其它的如推送的原理,流程什么的这里就不写了。 一. 去apple 开发者中心,创建app id。注意app id不...

本文只是记录一下如何在自己的电脑上配置apns推送环境,其它的如推送的原理,流程什么的这里就不写了。

一. 去apple 开发者中心,创建app id。注意app id不能使用通配符。并注意添加push notification service

     对于已经创建的app id,也可以编辑给他添加push notification service

二. 创建development 和 production的certificates及profiles.

  步骤略。

  注意

  1. 创建profile的时候app id及certification要正确。对于已经创建的profile也可以再次编辑修改其证书及devices。修改后只需要到xcode => references => accounts中refresh就可以了。

  2. 创建证书的时候我们会用keychain先在电脑上创建一个 .certsigningrequest文件,这个文件请保存,因为在证书到期后如果不用这个文件去更新,而用一个新的.certsigningrequest文件,那服务器需要使用的证书就又需要按照以下步骤重新生成。

三. 创建证书给服务器使用

  1. 在keychain中导出对应证书的private key。(方便后面使用,记为push.p12)

  2. openssl x509 -in aps_developer_identity.cer -inform der -out pushchatcert.pem

  3. openssl pkcs12 -nocerts -out pushchatkey.pem -in push.p12

  4. cat pushchatcert.pem pushchatkey.pem > ck.pem

四. 为了测试证书是否工作,执行下面的命令:

复制代码 代码如下:
$ telnet gateway.sandbox.push.apple.com 2195

  trying 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.pem

  enter pass phrase for pushchatkey.pem:


  你会看到一个完整的输出,让你明白 openssl 在后台做什么。如果连接是成功的,你可以键入一些字符。
  当你按下回车后,服务就会断开连接。如果在建立连接时有问题,openssl 将会给你一个错误消息,
  ck.pem 文件就是我们需要得到 push 服务器 连接 apns 的文件。

五. 配置本地服务器

  1. 启用apache
  mac os x 10.5.6自带了apache 2.2.9,直接在命令行运行apachectl start,apache就搞定了。

  现在apache的主目录就是/libary/webserver/documents/,你可以在这目录里放置文件测试了。

  2. 启用php
  mac os x 10.5.6自带了php 5.2.6,我们需要做的就是将php加入apache中。

  修改/etc/apache2/httpd.conf中的 #loadmodule php5_module libexec/apache2/libphp5.so 为

  loadmodule php5_module libexec/apache2/libphp5.so

  然后将/etc/php.ini.default复制为/etc/php.ini。

  cp /etc/php.ini.default /etc/php.ini

  之后就可以按照自己的习惯修改php.ini的配置  

  比如将error_reporting = e_all & ~e_notice 修改为

  error_reporting = e_all

  最后,重启apache,可以在/libary/webserver/documents/目录中建立个phpinfo.php来测试了。
  sudo apachectl restart

  3. 将步骤四生成的ck.pem拷贝到/library/webserver/documents/

    4. 创建push.php文件,并拷贝到/libary/webserver/documents/

<?php
  // 这里是我们上面得到的devicetoken,直接复制过来(记得去掉空格)
//devicetoken 在测试版本和上线版本上不一样。  
  //lei ipod touch
  $devicetoken = 'f5b70734ea5f4b91c904544f75457d6ecb908488317e097abaab769e741e6752';
  // put your private key's passphrase here:
  $passphrase = '1111';
  // put your alert message here:
  $message = 'my first push test!';
  ////////////////////////////////////////////////////////////////////////////////
  $message = array('msg'=>'小小说阅读器','title'=>'小小说','url'=>'http://www.apple.com.cn');
  //$message = array('msg'=>'去商品详细页面','itemtype'=>'2','id'=>'192172');
  //$message = array('msg'=>'去菜单页面','itemtype'=>'1','zktype'=>'1','order'=>'1','zksubtype'=>'1','zktitle'=>'9.9包邮');
  //$message = array('msg'=>'软件升级');
  $ctx = stream_context_create();
  stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  var_dump($ctx);
  // 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' => '逗你玩!哈哈。',
             'sound' => 'beep.wav',
             'badge' => 1
             );
  $body['type']=2;
  $body['data']=$message;
  // 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;
  else
  echo 'message successfully delivered' . php_eol;
  // close the connection to the server
  fclose($fp);
  ?>

  注意:代码中的devicetoken需要在真机上运行起来后,拷贝出来替换。

  重启apache,  sudo apachectl restart

  这样当我们访问一次就会收到一个通知。