解决方案 »

  1.   


    <?phpclass Push
    {
        public $deviceToken;
        public $localcert = 'ck.pem';
        public $passphrase = '11111';
        private function createPayload($message, $type, $sound, $number, $mid)
        {
            $body = array("aps" => array("alert" => $message, "badge" => 1, "sound" => 'received5.caf'));
            // Encode the payload as JSON
            $payload = json_encode($body);        return $payload;
        }    // Put your private key's passphrase here:
        public function  pushData($message, $type, $sound, $number, $mid)
        {
            $pem = dirname(dirname(__FILE__)) . '/' . $this->localcert;
            if (!file_exists($pem)) {
                echo '沒有找到密匙文件!' . PHP_EOL;
                exit;
            }
            //echo $pem . PHP_EOL;//debug        $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
            stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase);        // Open a connection to the APNS server
            //这个为正是的发布地址
            //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
        
            $serverURL = "ssl://gateway.push.apple.com:2195";
            $sanBoxURL = "ssl://gateway.sandbox.push.apple.com:2195";
            $fp = stream_socket_client($serverURL, $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;        foreach ($message as $key => $value) {
                $this->deviceToken = $key;
                $payload = $this->createPayload($value, $type, $sound, $number, $mid);            // Build the binary notification
                echo 'Sendto:' . $this->deviceToken . PHP_EOL;
                $msg = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $this->deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
                // Send it to the server
                $result = fwrite($fp, $msg, strlen($msg));
            }        // Close the connection to the server
            fclose($fp);        if (!$result) {
                return FALSE;
            } else {
                return TRUE;
            }
        }
    }?>拿去用吧,之前一个项目用到的推送类
      

  2.   

    badge是收到push時,app icon上顯示的數字。
    你的程序測試過可以發到push,如果發不到,請檢查pem證書和返回的狀態碼。
      

  3.   

    其实你可以改变一下思路!触发式:
    手机访问服务器接口时,自动查询是否有推送信息,有就读取提示,没有直接PASS。比你现在这个主动群发强,主动群发肯定知道手机已经连过服务器接口了,知道手机在线了!当然,这个只是个人意见!