解析php做推送服务端实现ios消息推送
程序员文章站
2023-11-15 12:32:40
准备工作1.获取手机注册应用的devicetoken(iphone手机注册应用时返回唯一值devicetoken)2.获取ck.pem文件(做手机端的给)3.获取pass...
准备工作
1.获取手机注册应用的devicetoken(iphone手机注册应用时返回唯一值devicetoken)
2.获取ck.pem文件(做手机端的给)
3.获取pass phrase(做手机端的给)
testpush.php文件
<?php
//手机注册应用返回唯一的devicetoken
$devicetoken = '6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 38a8aef9';
//ck.pem通关密码
$pass = 'jetson';
//消息内容
$message = 'a test message!';
//badge我也不知是什么
$badge = 4;
//sound我也不知是什么(或许是推送消息到手机时的提示音)
$sound = 'duck.wav';
//建设的通知有效载荷(即通知包含的一些信息)
$body = array();
$body['id'] = "4f94d38e7d9704f15c000055";
$body['aps'] = array('alert' => $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<br/>";
}
// 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);
?>
1.获取手机注册应用的devicetoken(iphone手机注册应用时返回唯一值devicetoken)
2.获取ck.pem文件(做手机端的给)
3.获取pass phrase(做手机端的给)
testpush.php文件
复制代码 代码如下:
<?php
//手机注册应用返回唯一的devicetoken
$devicetoken = '6ad7b13f b05e6137 a46a60ea 421e5016 4b701671 cc176f70 33bb9ef4 38a8aef9';
//ck.pem通关密码
$pass = 'jetson';
//消息内容
$message = 'a test message!';
//badge我也不知是什么
$badge = 4;
//sound我也不知是什么(或许是推送消息到手机时的提示音)
$sound = 'duck.wav';
//建设的通知有效载荷(即通知包含的一些信息)
$body = array();
$body['id'] = "4f94d38e7d9704f15c000055";
$body['aps'] = array('alert' => $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<br/>";
}
// 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);
?>
上一篇: Linux网络相关配置文件
下一篇: 在Word2010文档中改变字体颜色