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

php从苹果apns的feedback服务器获取推送失败的token

Jun 06, 2016 pm 08:13 PM
apns feedback php Push server apple Obtain

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

注意事项:

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

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

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

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

下面是我的代码:

Copy after login
Copy after login
<?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');
}
Copy after login
Copy after login
Copy after login
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Download the official website of Ouyi Exchange app for Apple mobile phone Download the official website of Ouyi Exchange app for Apple mobile phone Apr 28, 2025 pm 06:57 PM

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

gate.io sesame door latest official app address gate.io sesame door latest official app address Apr 22, 2025 pm 01:03 PM

The official Gate.io APP can be downloaded in the following ways: 1. Visit the official website gate.io to download; 2. Search "Gate.io" on the App Store or Google Play to download. Be sure to download it through the official channel to ensure safety.

Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Apr 28, 2025 pm 07:51 PM

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.

Summary of the top ten Apple version download portals for digital currency exchange apps Summary of the top ten Apple version download portals for digital currency exchange apps Apr 22, 2025 am 09:27 AM

Provides a variety of complex trading tools and market analysis. It covers more than 100 countries, has an average daily derivative trading volume of over US$30 billion, supports more than 300 trading pairs and 200 times leverage, has strong technical strength, a huge global user base, provides professional trading platforms, secure storage solutions and rich trading pairs.

A list of top ten virtual currency trading platforms that support multiple currencies A list of top ten virtual currency trading platforms that support multiple currencies Apr 22, 2025 am 08:15 AM

Priority is given to compliant platforms such as OKX and Coinbase, enabling multi-factor verification, and asset self-custody can reduce dependencies: 1. Select an exchange with a regulated license; 2. Turn on the whitelist of 2FA and withdrawals; 3. Use a hardware wallet or a platform that supports self-custody.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Binance official website entrance Binance official latest entrance 2025 Binance official website entrance Binance official latest entrance 2025 Apr 28, 2025 pm 07:54 PM

Visit Binance official website and check HTTPS and green lock logos to avoid phishing websites, and official applications can also be accessed safely.

See all articles