tp整合極光推送解決辦法
來源:互聯網
上載者:User
tp整合極光推送
有哪位大神做過極光推送? 麻煩有沒有代碼可以給我研究一下。。
在網上找了一個大神發的源碼 但是不知道怎麼放到tp架構中。。稍後奉上代碼 哪位能指導一下 先謝謝了
另:我想問一下 假如我自己寫了一個函數 叫jpush.php 那我如何在控制器中調用它? 這個檔案要放到哪個檔案夾下?
jpush.php
/**
* 極光推送
* @author 晨曦
* @Email [email protected]
* @Website http://www.jakehu.me/
* @version 20130706
*/
class jpush {
private $_masterSecret = '';
private $_appkeys = '';
/**
* 建構函式
* @param string $username
* @param string $password
* @param string $appkeys
*/
function __construct($masterSecret = '',$appkeys = '') {
$this->_masterSecret = $masterSecret;
$this->_appkeys = $appkeys;
}
/**
* 類比post進行url請求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $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);
$data = curl_exec($ch);//運行curl
curl_close($ch);
return $data;
}
/**
* 發送
* @param int $sendno 發送編號。由開發人員自己維護,標識一次發送請求
* @param int $receiver_type 接收者類型。1、指定的 IMEI。此時必須指定 appKeys。2、指定的 tag。3、指定的 alias。4、 對指定 appkey 的所有使用者推送訊息。
* @param string $receiver_value 發送範圍值,與 receiver_type相對應。 1、IMEI只支援一個 2、tag 支援多個,使用 "," 間隔。 3、alias 支援多個,使用 "," 間隔。 4、不需要填
* @param int $msg_type 發送訊息的類型:1、通知 2、自訂訊息
* @param string $msg_content 發送訊息的內容。 與 msg_type 相對應的值
* @param string $platform 目標使用者終端手機的平台類型,如: android, ios 多個請使用逗號分隔
*/
function send($sendno = 0,$receiver_type = 1, $receiver_value = '', $msg_type = 1, $msg_content = '', $platform = 'android,ios') {
$url = 'http://api.jpush.cn:8800/sendmsg/v2/sendmsg';
$param = '';
$param .= '&sendno='.$sendno;
$appkeys = $this->_appkeys;
$param .= '&app_key='.$appkeys;
$param .= '&receiver_type='.$receiver_type;
$param .= '&receiver_value='.$receiver_value;
$masterSecret = $this->_masterSecret;
$verification_code = md5($sendno.$receiver_type.$receiver_value.$masterSecret);
$param .= '&verification_code='.$verification_code;
$param .= '&msg_type='.$msg_type;
$param .= '&msg_content='.$msg_content;
$param .= '&platform='.$platform;
$res = $this->request_post($url, $param);
if ($res === false) {
return false;
}
$res_arr = json_decode($res, true);
return $res_arr;
}
}
?>
send.php
/**
* 極光推送
* @author 晨曦
* @Email [email protected]
* @Website http://www.jakehu.me/
* @version 20130706
*/
include('jpush.php');
$n_title = '1231232';
$n_content = '12312321231232123123212312321231232';
$appkeys='123123212312321231232';
$masterSecret='1231232123123212312321231232';
$sendno = 4;
$receiver_value = '';
$platform = 'android,ios' ;
$msg_content = json_encode(array('n_builder_id'=>0, 'n_title'=>$n_title, 'n_content'=>$n_content));
$obj = new jpush($masterSecret,$appkeys);
$res = $obj->send($sendno, 4, $receiver_value, 1, $msg_content, $platform);
print_r($res);
exit();
?>
------解決思路----------------------
sdk中的類檔案放到vendor目錄
自訂函數知道在哪吧...
在自訂函數裡tp的函數vendor('jiguang.jpush')引入
執行個體化時需要$a = new \jpush();