当前位置:Gxlcms > PHP教程 > Php推送IOS这段代码有有关问题吗

Php推送IOS这段代码有有关问题吗

时间:2021-07-01 10:21:17 帮助过:6人阅读

Php 推送IOS 这段代码有问题吗?
大家看下 这个代码 对不对?
	function tuisong(){
//手机注册应用返回唯一的deviceToken
$deviceToken = '7146a15ef9e1ebb8122c110e6fc970a454db3a9946ce24402481a564ee900e';
$deviceToken = $deviceToken;
//ck.pem通关密码
$pass = 'yeyu'; //$pass = '123456';
//消息内容
$message = 'zao shang hao!';
//badge我也不知是什么
$badge = 4;
//sound我也不知是什么(或许是推送消息到手机时的提示音)
$sound = 'Duck.wav';
//建设的通知有效载荷(即通知包含的一些信息)
$body = array();
//$body['id'] = "4f94d38e7d9704f15c000055";
$body['aps'] = array('alert' => $message,'issystem'=>1);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
//把数组数据转换为json数据
$payload = json_encode($body);
print_r($payload);
//echo strlen($payload),"\r\n";

//下边的写法就是死写法了,一般不需要修改,
//唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195这个是沙盒测试地址,ssl://gateway.push.apple.com:2195正式发布地址

$apnscert=$_SERVER['DOCUMENT_ROOT'].'data/ck.pem';
print_r($apnscert);
$ctx = stream_context_create();
var_dump($ctx);
//stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnscert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
//var_dump($fp);
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(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
}


------解决方案--------------------


class 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

人气教程排行