php
為什麼php做ios推送的伺服器的時候,後台啟動並執行時候會推送兩條資訊?但是手機關掉螢幕推送的時候就正常了~ 就只有後台啟動並執行時候是兩條?? 怎麼改呢?
下面是代碼
/**手機推送資訊類@author:wtt*/class Push{ private $deviceToken; private $message; function __construct($deviceToken,$message){ $this->deviceToken = $deviceToken; $this->message = $message; } public function push(){ //ck.pem通關密碼 $pass = '123456'; //訊息內容 //badge我也不知是什麼 $badge = 1; //sound我也不知是什麼(或許是推送訊息到手機時的提示音) $sound = 'Duck.wav'; //建設的通知有效載荷(即通知包含的一些資訊) $body = array(); $body['aps'] = array('alert' => $this->message); if ($badge) $body['aps']['badge'] = $badge; if ($sound) $body['aps']['sound'] = $sound; //把數組資料轉換為json資料 $payload = json_encode($body); //echo strlen($payload),"\r\n"; //下邊的寫法就是死寫法了,一般不需要修改, //唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195這個是沙箱測試地址,ssl://gateway.push.apple.com:2195正式發布地址 $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); if (!$fp) { print "Failed to connect {$err} {$errstr}\n"; return; } else { // print "Connection OK\n
"; } // send message $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $this->deviceToken)) . pack("n",strlen($payload)) . $payload; //print "Sending message :" . $payload . "\n"; fwrite($fp, $msg); fclose($fp); }}