• 代码千万行,注释第一行。命名不规范,同事泪千行。

PHP发送极光推送类

PHP Secondar 10个月前 (12-06) 461次浏览 未收录 0个评论

最近因需要开发一款APP需要推送消息,最终选用极光推送,以下是又php发送推送方法

<?php
/**
 * 
 */
class Ajpush {
	/**
	 * [push_send description]
	 * @author	secondar
	 * @copyright	(c) 2018
	 * @param  [type] $receive  		[通知栏标题]
	 * @param  [type] $content  		[通知子栏标题]
	 * @param  [type] $platform 		[目标用户终端手机的平台类型android,ios,winphone]
	 * @param  [type] $extras  	 		[消息体,支持数组]
	 * @param  [type] $m_time   		[保存离线时间的秒数]
	 * @param  [type] $alert_type		[通知形式,-1震动铃声,呼吸灯,2仅震动,详细查看极光推送文档]
	 * @param  [type] $sound			[ios通知铃声默认default]
	 * @return [type]           		[json_encode]
	 */
	public	function push_send($receive, $content, $platform,$extras=array(),$m_time,$alert_type=-1,$sound="default") {
		Global $arrGWeb;
	    $appkey = $arrGWeb['PushAppKey'];//'494b7ec55614299f158a4b6e'; //AppKey
	    $secret = $arrGWeb['PushSecret'];//'5cf7ced4e7988a0b38d1e9f9'; //Secret
		// return $appkey.$secret;
	    $postUrl = "https://api.jpush.cn/v3/push";

	    $base64 = base64_encode("$appkey:$secret");
	    $header = array("Authorization:Basic $base64", "Content-Type:application/json");
	    $data = array();
	    $data['platform'] = $platform;          //目标用户终端手机的平台类型android,ios,winphone
	    $data['audience'] = $receive;      //目标用户

	    $data['notification'] = array(
	        //统一的模式--标准模式
	        "alert" => $content,
	        //安卓自定义
	        "android" => array(
	            "alert" => $content,
	            "title" => "",
	            "alert_type"=>$alert_type,
	            "builder_id" => 1,
	            "extras" => $extras
	        ),
	        //ios的自定义
	        "ios" => array(
	            "alert" => $content,
	            "badge" => "1",
	            "sound" => $sound,
	            "extras" => $extras
	        )
	    );

	    //苹果自定义---为了弹出值方便调测
	    $data['message'] = array(
	        "msg_content" => $content,
	        "extras" => $extras
	    );

	    //附加选项
	    $data['options'] = array(
	        "sendno" => time(),
	        "time_to_live" => $m_time, //保存离线时间的秒数默认为一天
	        "apns_production" => false, //布尔类型   指定 APNS 通知发送环境:0开发环境,1生产环境。或者传递false和true
	    );
	    $param = json_encode($data);
	//    $postUrl = $this->url;
	    $curlPost = $param;

	    $ch = curl_init();                                      //初始化curl
	    curl_setopt($ch, CURLOPT_URL, $postUrl);                 //抓取指定网页
	    curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
	    curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
	    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);           // 增加 HTTP Header(头)里的字段
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	    $return_data = curl_exec($ch);                                 //运行curl
	    curl_close($ch);

	    return $return_data;
	}
}

?>

 

调用方法

引入文件后 $objAjpush = new Ajpush(); 实例化

//$receive = array('tag'=>array('tags');//标签
//$receive = array('registration_id'=>array($arrUserInfo['strationid']));//指定设备
$receive = 'all';//全部
$content = '信息中心提醒:您有一条待确认的信息';
$m_type = 'type';//给程序判断用,可有可无
$m_txt = '消息体';
$m_time = 84000;//离线保留时间
$objAjpush = new Ajpush();
$objAjpush->push_send($receive, $content ,'android,ios',array("type"=>$m_type, "txt"=>$m_txt),$m_time,2);

 

附下载地址:https://pan.baidu.com/s/1fm-dhYDESNRg_x9rt5HHGQ


一只大萝北 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP发送极光推送类
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址