php 使用 极光推送 类

时间:2023-12-11 18:47:32
<?php
/**
* 安卓和苹果消息主推
*/
class JPush extends CComponent
{ private $_push_body = array();
private $_android_body = array();
private $_ios_body = array();
const PUSH_URL = 'https://api.jpush.cn/v3/push';
const REPORT_URL = 'https://report.jpush.cn/v2/received';
const VALIDATE_URL = 'https://api.jpush.cn/v3/push/validate';
const CONNECT_TIMEOUT = 5;
const READ_TIMEOUT = 30;
const DEFAULT_MAX_RETRY_TIMES = 3;
private $_header = '';
private $_auth_basic = '';
private $_app_key = 'a7d627a70f752f32de5a0939';
private $_master_secret = 'f02725c2a9802badb60ab312';
public $retryTimes; public function __construct($retryTimes = self::DEFAULT_MAX_RETRY_TIMES, $appKey = '', $masterSecret = '')
{
$this->retryTimes = $retryTimes; if(!empty($appKey))
$this->_app_key = $appKey;
if(!empty($masterSecret))
$this->_master_secret = $masterSecret; //base64(appKey:masterSecret)
$this->_auth_basic = base64_encode($this->_app_key . ':' . $this->_master_secret);
//$this->_auth_basic = $this->_app_key . ':' . $this->_master_secret;
$this->_push_body = array(
'platform' => 'ios',// 推送平台设置 必填
'audience' => 'all', //推送设备指定 必填
//'notification' => array(), // 通知内容体。是被推送到客户端的内容。与 message 一起二者必须有其一,可以二者并存 可选
//'message' => array(), //推送参数 可选
'options' => array('time_to_live' => 30, 'apns_production' => false), //推送参数 可选
);
$this->_ios_body = array(
'alert' => '',
'sound' => 'ding.caf',
'badge' => '+1',
);
$this->_android_body = array(
'alert' => '',
'title' => 'send to android',
'builder_id' => 1,
);
$this->_header = array(
'Connection: Keep-Alive',
'Charset: UTF-8',
'Content-Type: application/json',
'Authorization: Basic ' . $this->_auth_basic,
); } /**
* notification android
*/
public function setAndroid($alert, $title = '', $builder_id = '')
{
$this->_android_body['alert'] = $alert;
if(!empty($title))
$this->_android_body['title'] = $title;
if(!empty($builder_id))
$this->_android_body['builder_id'] = $builder_id; $this->_push_body['notification']['android'] = $this->_android_body; }
/**
* notification ios
*/
public function setIos($alert, $sound = '', $badge = '')
{
$this->_ios_body['alert'] = $alert;
if(!empty($sound))
$this->_ios_body['sound'] = $sound;
if(!empty($badge))
$this->_ios_body['badge'] = $badge;
$this->_push_body['notification']['ios'] = $this->_ios_body;
}
/**
* set extras
*/
public function setExtras($extras)
{
if(!empty($extras) && is_array($extras)) {
if(!empty($this->_push_body['notification']['ios']))
$this->_push_body['notification']['ios']['extras'] = $extras;
if(!empty($this->_push_body['notification']['android']))
$this->_push_body['notification']['android']['extras'] = $extras;
}
} /**
* set options
*/
public function setOptions($options)
{
$this->_push_body['options'] = $options;
} /**
* 设置 推送
*/
public function push($platform = '', $audience = '')
{
if(!empty($platform)) {
$this->_push_body['platform'] = $platform;
}
if(!empty($audience)) {
$this->_push_body['audience'] = $audience;
} $data = json_encode($this->_push_body);
$res = $this->curlPost(self::PUSH_URL, $data, $this->_header); return $res;
} /**
* curl post
*/
public function curlPost($url, $data, $header = array())
{
$ch = curl_init();
if(!empty($header))
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
return $result;
}
/**
* curl get
*/
public function curlGet($url, $data, $header = array())
{
$ch = curl_init();
if(!empty($header))
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean(); return $result;
} }