解决方案 »
- 如何用php给另一个php文件里写html标签
- php mysql 高手来看解决一个辣手的问题,同一表中输出数组不知为何只显示一组?
- 能不能使用模板里定义的变量
- 在一个查询语句中使用多个合计函数返回一个值
- 累了,给大家看看小弟弄的
- 写过文章系统的请进, 怎么才能删除一个文章类别时,同时也将此类及子类的文章全部删除?
- 如何将 Session 存放在数据库中,进行跨域名!(求源码)
- 进度条问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 关于在IIS5.0下配置PHP
- 如何连接DB数据库!!
- 怎么获得函数本身有多少个参数?
- 不同浏览器下,$_SERVER数组顺序为什么不一样
<?phpclass 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 | STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL); //echo 'Connected to APNS' . PHP_EOL; foreach ($message as $key => $value) {
$this->deviceToken = $key;
$payload = $this->createPayload($value, $type, $sound, $number, $mid); // Build the binary notification
echo 'Sendto:' . $this->deviceToken . PHP_EOL;
$msg = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $this->deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
} // Close the connection to the server
fclose($fp); if (!$result) {
return FALSE;
} else {
return TRUE;
}
}
}?>拿去用吧,之前一个项目用到的推送类
你的程序測試過可以發到push,如果發不到,請檢查pem證書和返回的狀態碼。
手机访问服务器接口时,自动查询是否有推送信息,有就读取提示,没有直接PASS。比你现在这个主动群发强,主动群发肯定知道手机已经连过服务器接口了,知道手机在线了!当然,这个只是个人意见!