$send_push_list=get_ids($token_list,'app_id');
$deviceToken =$send_push_list;//隐藏了自己的token
//ck.pem通关密码
$pass = '****200825';//最新ios 密码
//消息内容
$message['title'] = $msg['title'];
$message['body'] = $msg['body'];
//建设的通知有效载荷(即通知包含的一些信息)
$body = array();
$body['aps'] = array(
'alert' => $message,
'badge'=>6,
'sound'=>!empty($msg['sound'])?$msg['sound']:'' //default : 系统声音,0或者空:静音 default
);
$data['deep_link']=urlencode($data['deep_link']);
$body['deep_link']=$data['deep_link'];
$body['other_para']=$data['other_para'];;
$data['message_id']=$message_id; // 建议最好不要重复
//把数组数据转换为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();
$pem = dirname(__FILE__) .'/'.'ApnsDev.pem'; //秘钥文件,需要ios开发工具打包生成。
//echo 'Pem文件:'.$pem.'<br/>';
//echo 'ios 口令:'.$v.'<br/>';
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
//测试地址
//$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
//正式地址
$fp = stream_socket_client('tls://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
//print "Failed to connect $err $errstr\n";
return "Failed to connect $err $errstr";
}
else {
print "Connection OK\n<br/>";
}
foreach ($deviceToken as $k=>$v){
// send message
$push_msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $v))) . pack("n",strlen($payload)) . $payload; //循环写入ios设备的app id
//print "Sending message :" . $payload . "\n";
fwrite($fp, $push_msg);
}
fclose($fp);
参考文档:
IT起义网络科技








