极光推送Jpush(v2)服务端PHP版本的api脚本类

  •   
  • 8311
  • PHP
  • 6
  • super_dodo
  • 2014/08/19

在许多的手机App开发中推送是一个必须的应用。高大上的企业都会自己投入成本搭建自己的推送服务器,而小微企业则会选择一些服务商,使用他们的服务,减轻自己的运营和维护的成本。Jpush(极光推送)是目前比较火辣辣的一个推送服务商。对于初级用户可以免费使用。每分钟600次推送。对于小企业或者测试中的产品来说,基本上满足需求了。关于极光推送的相关文档或者简介,请移步极光推送官网 https://www.jpush.cn/

这里适用的版本是v2版本的php接口api类。只需实例化之后传入指定参数即可实现推送。直接上代码。

//极光推送的类
//文档见:http://docs.jpush.cn/display/dev/Push+API+v2#PushAPIv2-V

/***使用示例
	$pushObj = new Jpush;
	//组装需要的参数
	$type = '2';	//2指定的tag 3指定的 alias 4广播
	$receive = '2401';
	$content = '爱曲靖标签推送....';
	$m_type = 'http';
	$m_txt = 'http://www.iqujing.com/';

	//调用推送,并处理
	$result = $pushObj->push($type,$receive,$content,$m_type,$m_txt);	
	$rt = json_decode($result);
	if($rt->errcode == '0'){		//推送成功
		//处理成功的推送......
	}else{							//推送失败
		//处理失败的推送...失败的信息 在  $rt->errmsg 里
	}

***/

class Jpush{

	public $app_key = 'd7fd***********c3642fc';			//待发送的应用程序(appKey),只能填一个。
	public $master_secret = 'a04**********4a80377';		//主密码
	public $url = "http://api.jpush.cn:8800/v2/push";		//推送的地址
	
	//若实例化的时候传入相应的值则按新的相应值进行
	public function __construct($app_key=null, $master_secret=null,$url=null) {
		if ($app_key) $this->app_key = $app_key;
		if ($master_secret) $this->master_secret = $master_secret;
		if ($url) $this->url = $url;
	}

	//$type 推送接收的类型 2.指定的Tag 3.指定的alias 4.广播:对app_key下的所有用户推送消息
	//$receiver 接收者的信息 2.支持多达10个 3.支持多达1000个 4.不用填
	//$content 推送的内容。
	//$m_type 推送附加字段的类型(可不填) http,tips,chat....
	//$m_txt 推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。
	public function push($type,$receiver,$content,$m_type='',$m_txt=''){
		$data = array();
		$data['sendno'] = time();
		$data['app_key'] = $this->app_key;
		$data['receiver_type'] = $type;			//推送接收的类型	
		$data['receiver_value'] = $receiver;
		$data['verification_code'] = strtoupper(md5( $data['sendno'].$data['receiver_type'].$data['receiver_value'].$this->master_secret));
		$data['msg_type'] = 1;			//1 通知(默认都是通知) 2 自定义消息
		$data['msg_content'] = json_encode(array(
			'n_builder_id'=>0,			//使用 极光Push SDK 的默认通知样式
			'n_title'=>'',				//不填则默认为应用的名称
			'n_content'=>$content,		//通知的内容
			'n_extras'=>array(
				"ios"=>array("badge"=>1, "sound"=>"default", "content-available"=>1),
				'type'=>$m_type,
				'txt'=>$m_txt,
			)
		));
		$data['platform'] = 'android,ios,winphone';		//目标用户终端手机的平台类型
		$data['apns_production'] = 1;					//指定 APNS 通知发送环境:0: 开发环境,1:生产环境。
		if($type == '3'){		//点推的离线时间	
			$data['time_to_live'] = 600;					//点推离线时间10分钟,聊天
		}else{
			$data['time_to_live'] = 86400;					//保存离线时间的秒数默认为一天
		}
		$pushData = array();
		foreach($data as $k=>$v){
			$pushData[] = ($k .'=' . urlencode($v));
		}
		$pushData = implode('&',$pushData);

		$html = $this->pushCurl($this->url,$pushData);
		return $html;			//返回发送的状态返回信息
	}


	//执行CURL
	public function pushCurl($url,$pushData){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
		curl_setopt($ch, CURLOPT_POST, true); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,$pushData);
		$html = curl_exec($ch);
		curl_close($ch);
		return $html;
	}


}

后面我还会更新v3的版本的接口类,供大家学习参考。也请大家提出指正意见和改进方法,共同学习进步。

知而必行,行而必恒,恒而必达!