时间:2021-07-01 10:21:17 帮助过:14人阅读
为什么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); }}