php - 在 apns.php 中获取失败的设备 token

标签 php mysql arrays apple-push-notifications apns-php

大家好, 这是我的 Apns.php 文件。

      <?php
      //error_reporting(E_ALL);
      # -*- coding: utf-8 -*-
      ##
      ##     Copyright (c) 2010 Benjamin Ortuzar Seconde <bortuzar@gmail.com>
      ##
      ##     This file is part of APNS.
      ##
      ##     APNS is free software: you can redistribute it and/or modify
      ##     it under the terms of the GNU Lesser General Public License as
      ##     published by the Free Software Foundation, either version 3 of
      ##     the License, or (at your option) any later version.
      ##
      ##     APNS is distributed in the hope that it will be useful,
      ##     but WITHOUT ANY WARRANTY; without even the implied warranty of
      ##     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      ##     GNU General Public License for more details.
      ##
      ##     You should have received a copy of the GNU General Public License
      ##     along with APNS.  If not, see <http://www.gnu.org/licenses/>.
      ##
      ##
      ## $Id: Apns.php 168 2010-08-28 01:24:04Z Benjamin Ortuzar Seconde $
      ##

      /**
       * Apple Push Notification Server
       */
      class Apns
      {
          protected $server;
          protected $keyCertFilePath;
          protected $passphrase;
          protected $stream;


              /**
               * Connects to the APNS server with a certificate and a passphrase
               *
               * @param <string> $server
               * @param <string> $keyCertFilePath
               * @param <string> $passphrase
               */
          function __construct($server, $keyCertFilePath ,$passphrase){

              $this->server = $server;
              $this->keyCertFilePath = $keyCertFilePath;
              $this->passphrase = $passphrase;
              $this->connect();
          }


          /**
               * Connects to the server with the certificate and passphrase
               *
               * @return <void>
               */
          private function connect(){

              $ctx = stream_context_create();
              stream_context_set_option($ctx, 'ssl', 'local_cert', $this->keyCertFilePath);
              // assume the private key passphase was removed.
              stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase);
              $this->stream = stream_socket_client($this->server, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
              if (!$this->stream) {
                  throw new Exception("<br/>Failed to connect $err $errstr");
              }
              else {
                  //print "<br/>Opening connection to: {$this->server}";
              }
          }
              /**
               * Sends a message to device
               * 
               * @param <string> $deviceToken
               * @param <string> $message
               * @param <int> $badge
               * @param <string> $sound
               */
              public function sendMessage($deviceToken, $message, $badge = NULL, $sound = NULL,$uid=NULL,$type=NULL,$sid=NULL,$io=NULL){

                  //generate the payload
                  $payload = $this->generatePayload($message, $badge, $sound,$uid,$type,$sid,$io);
                  //send payload to the device.
                  $this->sendPayload($deviceToken, $payload);
              }
              /*
               *
               * Generates the payload
               * 
               * @param <string> $message
               * @param <int> $badge
               * @param <string> $sound
               * @return <string>
               */
              protected function generatePayload($message, $badge = NULL, $sound = NULL,$uid=NULL,$type=NULL,$sid=NULL,$io=NULL) {
                  $body = array();
                   //message
                  $body['aps'] = array('alert' => $message);
                   //badge
                   if ($badge) {
                       $body['aps']['badge'] = $badge;
                   }
                    //sound
                   if ($sound) {
                        $body['aps']['sound']=$sound;
                  }
                  if ($uid) {
                        $body['aps']['uid'] = $uid;
                  }
                  if ($type) {
                        $body['aps']['type'] = $type;
                  }
                  if ($sid!=NULL) {
                      $body['aps']['sid'] = $sid;
                  }
                  if ($io!=NULL) {
                      $body['aps']['io'] = $io;
                  }
                  $payload = json_encode($body);

                  echo "<pre>"; print_r($payload);die;

                  return $payload;
              }
              /**
               * Writes the contents of payload to the file stream
               * 
               * @param <string> $deviceToken
               * @param <string> $payload
               */
          protected function sendPayload($deviceToken, $payload){
              $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
              //$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ','', $deviceToken)) . pack("n",strlen($payload)) . $payload;
              fwrite($this->stream, $msg);
          }
          /**
               * Gets an array of feedback tokens
               *
               * @return <array>
               */
          public function getFeedbackTokens() {
              $feedback_tokens = array();
              //and read the data on the connection:
              while(!feof($this->stream)) {
                  $data = fread($this->stream, 38);
                  if(strlen($data)) {      
                      $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);
                  }
              }
              return $feedback_tokens;
          }
          /**
               * Closes the stream
               */
          function __destruct(){
                //  print "<br/>Clossing connection to: {$this->server}";
                  fclose($this->stream);
          }
      }//end of class
      ?>
  • 我想查找哪个设备无法获取 IOS 通知。
  • 有些设备会收到通知,有些则无法收到。
  • 我想获取未收到通知的失败设备 token 。
  • 失败的设备 token 将插入到我的数据库中。我该怎么办?
  • 您能否帮助我并提供正确的解决方案来获取失败的设备 token 。 ?

  • 提前致谢!

最佳答案

Apple 不会告诉您以下信息:

  1. 不会判断消息是否发送成功 2.不会告知用户是否选择退出推送通知

关于php - 在 apns.php 中获取失败的设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40713924/

相关文章:

php - 带有换行符示例的 Android SAX 解析器?

php - 引用 : Why are my "special" Unicode characters encoded weird using json_encode?

javascript - 无法显示每个元素的 Highcharts

php - 使用 AJAX 执行 SQL 查询并在成功执行后重定向

java - BufferedReader 并填充一个 int 数组

Java,如何将字符串转换为数组

php - MySQL -- fatal error : Call to a member function bind_param() on a non-object

php - Laravel 工厂辅助函数如何在命名空间方面工作?

mysql - 这是在MySQL中存储JSON的合理用例吗?

android - 如何从android上的字符串数组中读取项目