Home > php教程 > php手册 > 用php搭建apns推送服务器

用php搭建apns推送服务器

WBOY
Release: 2016-06-06 19:54:13
Original
1457 people have browsed it

是按照教程http://blog.csdn.net/newjueqi/article/details/7898591 来做的。 注意事项: 1. 测试阶段使用的推送地址: ssl://gateway.sandbox.push.apple.com:2195 正式上线使用的推送地址: ssl://gateway.push.apple.com:2195 2. 那里给的php推送代码有问

是按照教程 http://blog.csdn.net/newjueqi/article/details/7898591 来做的。


注意事项:

1. 测试阶段使用的推送地址:

ssl://gateway.sandbox.push.apple.com:2195


正式上线使用的推送地址:

ssl://gateway.push.apple.com:2195


2. 

那里给的php推送代码有问题,下面我贴出修改后推送成功的代码:


<?php // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
$deviceToken=$_POST['deviceToken'];

// Put your private key's passphrase here:
$passphrase = 'password';

// Put your alert message here:
$message = $_POST['message'];

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $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);
//这个是沙盒测试地址,发布到appstore后记得修改哦

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $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;
// Create the payload body

$body['aps'] = array(
'alert' => $message,
'forum_id' => 88,
'topic_id' => 999,
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
//$msg = chr(0).pack('n', 32).pack('H', $deviceToken). pack('n', strlen($payload)).$payload;
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result){
	echo 'Message not delivered' . PHP_EOL;
}else{
	echo 'Message successfully delivered' . PHP_EOL;
}

// Close the connection to the server
fclose($fp);

?>
Copy after login


[文章作者]曾健生

[作者邮箱]zengjiansheng1@126.com

[作者QQ]190678908

[博客]  http://blog.csdn.net/newjueqi

http://blog.sina.com.cn/h6k65


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template