<?php
namespace jpush;
require_once '../extend/jpush/autoload.php';
use JPush\Client as JgPush;
/**php
-
極光推送
*/
class jpush{
private $app_key = '56df4a96d1dea893ddb2ad0b'; // 待發送的應用程序(appKey),只能填一個。
private $master_secret = '265fc9ee9e823dfef610e07f'; // 主密碼android
//若實例化的時候傳入相應的值則按新的相應值進行
public function __construct($app_key=null, $master_secret=null) {
if ($app_key) $this->app_key = $app_key;
if ($master_secret) $this->master_secret = $master_secret;
}
/**ios
- 發送推送信息
- [send_msg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-30
- @param [type] $title [description] 標題
- @param [type] $alert [description] 內容
- @param array $extras [description] 擴展字段
- @param [type] $type [description] 類型 1 指定用戶 2 按別名 3 按標籤
- @param [type] $value [description] 用戶信息
- @param array $platform [description] 推送平臺
- @return [type] [description]
*/
public function send_msg($title,$alert,$extras=array(),$type,$value,$platform=array('android','ios')){
// 完整的推送示例,包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等
$client = new JgPush($this->app_key, $this->master_secret);
$android_notification = array(
'title' => $title,
'builder_id' => 2,
'extras' => $extras
);
$message = array(
'title' => $title,
'extras' => $extras
);
$ios_notification = array(
'badge' => 2, // 應用角標 爲 0 表示清除
'content-available' => true, // 推送喚醒 默認 true
'extras' => $extras
);
$push = $client->push();
if ($type == 1) {
$push_payload = $push
->setPlatform($platform)
->addRegistrationId($value)
->androidNotification($alert,$android_notification)
->iosNotification($alert,$ios_notification)
->message($alert, $message);
}elseif ($type == 2) {
$push_payload = $push
->setPlatform($platform)
->addAlias($value)
->androidNotification($alert,$android_notification)
->iosNotification($alert,$ios_notification)
->message($alert, $message);
}elseif ($type == 3) {
$push_payload = $push
->setPlatform($platform)
->addTag($value)
->androidNotification($alert,$android_notification)
->iosNotification($alert,$ios_notification)
->message($alert, $message);
}
try {
$response = $push_payload->send();
$msg = $this->get_msg($response['http_code']);
if ($response['http_code'] == 200) {
$ret = ['code'=>1,'data'=>$response['body'],'msg'=>$msg];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>$msg];
}
}catch (\JPush\Exceptions\APIConnectionException $e) {
$ret = ['code'=>0,'data'=>'','msg'=>$e];
} catch (\JPush\Exceptions\APIRequestException $e) {
$ret = ['code'=>0,'data'=>'','msg'=>$e];
}
return $ret;
}
/**
- 獲取返回信息
- [get_msg description]
- @Author 念天地之悠悠
- @DateTime 2019-12-30
- @param [type] $code [description] 返回狀態碼
- @return [type] [description]
*/
public function get_msg($code){
switch ($code) {
case 200:
$message = '發送成功!';
break;
case 1000:
$message = '失敗(系統內部錯誤)';
break;
case 1001:
$message = '失敗(只支持 HTTP Post 方法,不支持 Get 方法)';
break;
case 1002:
$message = '失敗(缺乏了必須的參數)';
break;
case 1003:
$message = '失敗(參數值不合法)';
break;
case 1004:
$message = '失敗(驗證失敗)';
break;
case 1005:
$message = '失敗(消息體太大)';
break;
case 1008:
$message = '失敗(appkey參數非法)';
break;
case 1020:
$message = '失敗(只支持 HTTPS 請求)';
break;
case 1030:
$message = '失敗(內部服務超時)';
break;
default:
$message = '失敗(返回其餘狀態,目前不清楚額,請聯繫開發人員!)';
break;
}
return $message;
}
/**
- 查詢設備的標籤與別名及手機號
- [getDevices description]
- @Author 念天地之悠悠
- @DateTime 2019-12-30
- @param [type] $registration_id [description] 設備標識
- @return [type] [description]
*/
public function getDevices($registration_id){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->getDevices($registration_id);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>$info['body'],'msg'=>'獲取成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'獲取失敗!'];
}
return $ret;
}
/**
- 更新別名(具備惟一性)
- [updateAlias description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description] 設備標識
- @param [type] $alias [description] 別名
- @return [type] [description]
*/
public function updateAlias($registration_id,$alias){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->updateAlias($registration_id,$alias);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>'','msg'=>'更新成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'更新失敗!'];
}
return $ret;
}
/**
- 新增標籤
- [addTags description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description] 設備標識
- @param [type] $tags [description] 別名 多個用數組 單個用字符串
*/
public function addTags($registration_id,$tags){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->addTags($registration_id,$tags);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>'','msg'=>'新增成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'新增失敗!'];
}
return $ret;
}
/**
- 移除標籤
- [removeTags description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description] 設備標識
- @param [type] $tags [description] 別名 多個用數組 單個用字符串
- @return [type] [description]
*/
public function removeTags($registration_id,$tags){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->addTags($registration_id,$tags);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>'','msg'=>'移除成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'移除失敗!'];
}
return $ret;
}
/**
- 刪除全部標籤
- [clearTags description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description] 設備標識
- @return [type] [description]
*/
public function clearTags($registration_id){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->clearTags($registration_id);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>'','msg'=>'移除成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'移除失敗!'];
}
return $ret;
}
/**
- 綁定手機號碼
- [updateMoblie description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description] 設備標識
- @param [type] $tel [description] 手機號碼
- @return [type] [description]
*/
public function updateMoblie($registration_id,$tel){
$client = new JgPush($this->app_key, $this->master_secret);
$device = $client->device();
$info = $device->updateMoblie($registration_id,$tel);
if ($info['http_code'] == 200) {
$ret = ['code'=>1,'data'=>'','msg'=>'綁定成功!'];
}else{
$ret = ['code'=>0,'data'=>'','msg'=>'綁定失敗!'];
}
return $ret;
}
/**
- 取消手機綁定
- [clearMobile description]
- @Author 念天地之悠悠
- @DateTime 2019-12-31
- @param [type] $registration_id [description]
- @return [type] [description]*/public function clearMobile($registration_id){$client = new JgPush($this->app_key, $this->master_secret);$device = $client->device();$info = $device->clearMobile($registration_id);if ($info['http_code'] == 200) {$ret = ['code'=>1,'data'=>'','msg'=>'取消綁定成功!'];}else{$ret = ['code'=>0,'data'=>'','msg'=>'取消綁定失敗!'];}return $ret;}}