search
Homephp教程php手册php从苹果apns的feedback服务器获取推送失败的token

注意事项: 1.建议和feedback服务器建立长连接,连接过于频繁有可能被当做攻击(简简单单的做一些测试时没有关系的); 2.获取的token是在上次你给你的应用发推送失败时加feedback服务的,里面会返回失败的具体时间 3.返回的数据由三部分组成,请看下面的图 结

注意事项:

1.建议和feedback服务器建立长连接,连接过于频繁有可能被当做攻击(简简单单的做一些测试时没有关系的);

2.获取的token是在上次你给你的应用发推送失败时加feedback服务的,里面会返回失败的具体时间

3.返回的数据由三部分组成,请看下面的图

1357647920_4440结构中包含三个部分,第一部分是一个上次发推送失败的时间戳,第二个部分是device_token的长度,第三部分就是失效的device_token

下面是我的代码:


<?php /**
 * @desc 连接苹果的推送服务器
 * @return bool|resource
 */
function connect_feedback(){
   $passphrase = 'cheshi123';
   $cert_file = 'cheshi_productionck.pem';//推送的证书地址,环境不要错了
   $feedback_server = 'ssl://feedback.push.apple.com:2196';//feedback服务器地址
   //沙盒环境地址是 ssl://feedback.sandbox.push.apple.com:2196
   $ctx = stream_context_create();
   stream_context_set_option($ctx, 'ssl', 'local_cert', $cert_file);
   stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
   $fp = stream_socket_client($feedback_server, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
   if(!$fp){
      print "Failed to connect feedback server: $error $errorString\n";
      return false;
   }else{
      print "Connection to feedback server OK\n";
   }
   return $fp;
}
/**
 * @desc 连接本地的redis,用来存储获取到的token,也可以用其他方式存储
 * @param $redis_server
 *
 * @return bool|Redis
 */
function connect_redis($redis_server){
   $param = parse_url($redis_server);
   $redis = new Redis();
   if($redis->connect($param['host'],$param['port'])){
      print "connect redis OK";
   }else{
      print "failed to connect redis server";
      return false;
   }
   return $redis;
}
/**
 * @desc 执行推送操作的主要代码
 */
function feedback(){
   $count1 = 0;
   $run_times = 0;
   $redis_server = "tcp://localhost:6379";
   $iostokenremoved= 'iostokenremoved';
   $iostokenremoved_num = 'iostokenremoved_num';
   $fp = connect_feedback();
   $redis = connect_redis($redis_server);
   //苹果建议provider和feedback服务维持一个长连接,如果频繁的建立连接可能会被当做攻击处理
   $devcon ='';
   while($run_timesping()==='+PONG'){
         print date('Ymd His').'|redis server disconnected';
         @$redis->close();
         unset($redis);
         $redis = connect_redis($redis_server);
      }
      //每次读取38个字段,这是保存的一个完整token 信息的长度
      while ($devcon = fread($fp, 38)){
         $count1 ++ ;
         $arr = unpack("H*", $devcon);//解包传过来的二进制数据
         $rawhex = trim(implode("", $arr));
         $feedbackTime = hexdec(substr($rawhex, 0, 8));
         $feedbackDate = date('Y-m-d H:i:s', $feedbackTime);
         $feedbackDeviceToken = substr($rawhex, 12, 64);
         //记录被删除的token
         $redis->hSet($iostokenremoved,$feedbackDeviceToken,$feedbackDate);
         //记录每天的卸载数量
         $redis->hIncrBy($iostokenremoved_num,date('Y-m-d',$feedbackTime),1);
      }
      echo 'FeedBack:'. $count1 . PHP_EOL;
      usleep(10000000);//sleep 10秒
   }
   $redis->close();
   fclose($fp);
}
//可以通过命令行来执行这个脚本 php -f feedback.php feedback
if(isset($argc[1]) && function_exists($argv[1])){
   $argv[1]();
}else{
   die('invalid input');
}

    
    
    


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment