欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

ios推送消息php做推送服务器

程序员文章站 2024-01-16 17:40:04
...
/**
* Main method to run the object
* $message 消息内容
* $deviceToken 这里是iphone手机唯一的Token码(记得去掉空格)
* $badge 就是应用图标右上角那个数字
* $sound 消息的声音
* $apnsCert 证书路径
* $passphrase 私钥的密码(可以不写)
*/
public function iosPush($message,$deviceToken,$badge=1,$sound='Duck.wav',$apnsCert,$passphrase){
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
$payload = json_encode($body);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print_r();
print "Failed to connect $err $errstr\n";
return;
}
else {
print $message;
print "Connection OK\n
";
}
$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);

}


?>