Example #1
0
 public function easy_push($id, $content = 'hello')
 {
     $br = '<br/>';
     $spilt = ' - ';
     $master_secret = '3dd1c77ac980516110338aa5';
     $app_key = 'ab36dbc34e0501604d25974e';
     JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
     $client = new JPushClient($app_key, $master_secret);
     $json = array('type' => 2);
     //echo $json;
     try {
         $result = $client->push()->setPlatform(M\all)->setAudience(M\alias(array($id)))->setNotification(M\notification($content, M\android('hi,android', $title = null, $builder_id = null, $extras = $json)))->printJSON()->send();
         /*$result = $client->push()
           ->setPlatform(M\all)
           ->setAudience(M\alias(array($id)))
           ->setNotification(M\notification($content))
           ->printJSON()
           ->send();*/
         echo 'Push Success.' . $br;
         echo 'sendno : ' . $result->sendno . $br;
         echo 'msg_id : ' . $result->msg_id . $br;
         echo 'Response JSON : ' . $result->json . $br;
     } catch (APIRequestException $e) {
         echo 'Push Fail.' . $br;
         echo 'Http Code : ' . $e->httpCode . $br;
         echo 'code : ' . $e->code . $br;
         echo 'Error Message : ' . $e->message . $br;
         echo 'Response JSON : ' . $e->json . $br;
         echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
         echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
         echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
     } catch (APIConnectionException $e) {
         echo 'Push Fail: ' . $br;
         echo 'Error Message: ' . $e->getMessage() . $br;
         //response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
         echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
     }
 }
Example #2
0
require "../include/get_data.php";
use JPush\Model as M;
use JPush\JPushClient;
use JPush\JPushLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;
$uid = $arr['uid'];
$msg = $arr['msg'];
$role = $arr['role'];
$master_secret_c = '24a6bb3051447e0b644021ef';
$app_key_c = '6cede177da188c2915f3dcb7';
$master_secret_p = 'c48e23b33f6a0065fce06e59';
$app_key_p = '0770fce0d05d7f5baf88dae7';
JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
if (isset($uid) && isset($msg) && isset($role)) {
    $rs_arr = $d2b->select("user_main", ["channelid"], ["uid" => $uid]);
    $channelid = $rs_arr[0]['channelid'];
    switch ($role) {
        case 1:
            $client = new JPushClient($app_key_c, $master_secret_c);
            try {
                $result = $client->push()->setPlatform(M\platform('ios', 'android'))->setAudience(M\audience(M\registration_id(array($channelid))))->setNotification(M\notification($msg, M\android($msg), M\ios($msg, 'happy', 1, true, null, 'THE-CATEGORY')))->setMessage(M\message($msg, null, null, array('key' => 'value')))->setOptions(M\options(123456, null, null, false, 0))->send();
                $return_arr = ["channelid" => $channelid];
                include "../include/return_data.php";
            } catch (APIRequestException $e) {
                echo 'code : ' . $e->code;
            }
            break;
        case 2:
Example #3
0
 /**
  * Index Page for this controller.
  *
  * Maps to the following URL
  * http://example.com/index.php/welcome
  * - or -
  * http://example.com/index.php/welcome/index
  * - or -
  * Since this controller is set as the default controller in
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  *
  * @see http://codeigniter.com/user_guide/general/urls.html
  */
 public function index()
 {
     $br = '<br/>';
     $spilt = ' - ';
     $master_secret = '3047c84ce72ed9ab392e6be1';
     $app_key = '57d93d27208ab6e2516b129d';
     JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
     $client = new JPushClient($app_key, $master_secret);
     // easy push
     try {
         $result = $client->push()->setPlatform(M\all)->setAudience(M\all)->setNotification(M\notification('Hi, JPush'))->printJSON()->send();
         echo 'Push Success.' . $br;
         echo 'sendno : ' . $result->sendno . $br;
         echo 'msg_id : ' . $result->msg_id . $br;
         echo 'Response JSON : ' . $result->json . $br;
     } catch (APIRequestException $e) {
         echo 'Push Fail.' . $br;
         echo 'Http Code : ' . $e->httpCode . $br;
         echo 'code : ' . $e->code . $br;
         echo 'Error Message : ' . $e->message . $br;
         echo 'Response JSON : ' . $e->json . $br;
         echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
         echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
         echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
     } catch (APIConnectionException $e) {
         echo 'Push Fail: ' . $br;
         echo 'Error Message: ' . $e->getMessage() . $br;
         // response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
         echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
     }
     echo $br . '-------------' . $br;
     // easy push with ios badge +1
     // 以下演示推送给 Android, IOS 平台下Tag为tag1的用户的示例
     try {
         $result = $client->push()->setPlatform(M\Platform('android', 'ios'))->setAudience(M\Audience(M\Tag(array('tag1'))))->setNotification(M\notification('Hi, JPush', M\android('Hi, Android', 'Message Title', 1, array("key1" => "value1", "key2" => "value2")), M\ios("Hi, IOS", "happy", "+1", true, array("key1" => "value1", "key2" => "value2"), "Ios8 Category")))->setMessage(M\message('Message Content', 'Message Title', 'Message Type', array("key1" => "value1", "key2" => "value2")))->printJSON()->send();
         echo 'Push Success.' . $br;
         echo 'sendno : ' . $result->sendno . $br;
         echo 'msg_id : ' . $result->msg_id . $br;
         echo 'Response JSON : ' . $result->json . $br;
     } catch (APIRequestException $e) {
         echo 'Push Fail.' . $br;
         echo 'Http Code : ' . $e->httpCode . $br;
         echo 'code : ' . $e->code . $br;
         echo 'Error Message : ' . $e->message . $br;
         echo 'Response JSON : ' . $e->json . $br;
         echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
         echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
         echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
     } catch (APIConnectionException $e) {
         echo 'Push Fail: ' . $br;
         echo 'Error Message: ' . $e->getMessage() . $br;
         // response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
         echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
     }
     echo $br . '-------------' . $br;
     // full push
     try {
         $result = $client->push()->setPlatform(M\platform('ios', 'android'))->setAudience(M\audience(M\tag(array('555', '666')), M\alias(array('555', '666'))))->setNotification(M\notification('Hi, JPush', M\android('Hi, android'), M\ios('Hi, ios', 'happy', 1, true, null, 'THE-CATEGORY')))->setMessage(M\message('msg content', null, null, array('key' => 'value')))->setOptions(M\options(123456, null, null, false, 0))->printJSON()->send();
         echo 'Push Success.' . $br;
         echo 'sendno : ' . $result->sendno . $br;
         echo 'msg_id : ' . $result->msg_id . $br;
         echo 'Response JSON : ' . $result->json . $br;
     } catch (APIRequestException $e) {
         echo 'Push Fail.' . $br;
         echo 'Http Code : ' . $e->httpCode . $br;
         echo 'code : ' . $e->code . $br;
         echo 'message : ' . $e->message . $br;
         echo 'Response JSON : ' . $e->json . $br;
         echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
         echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
         echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
     } catch (APIConnectionException $e) {
         echo 'Push Fail: ' . $br;
         echo 'Error Message: ' . $e->getMessage() . $br;
         // response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
         echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
     }
     echo $br . '-------------' . $br;
     // fail push
     try {
         $result = $client->push()->setPlatform(M\all)->setAudience(M\all)->setNotification(M\notification('Hi, JPush'))->setAudience(M\audience(array('no one')))->printJSON()->send();
         echo 'Push Success.' . $br;
         echo 'sendno : ' . $result->sendno . $br;
         echo 'msg_id : ' . $result->msg_id . $br;
         echo 'Response JSON : ' . $result->json . $br;
     } catch (APIRequestException $e) {
         echo 'Push Fail.' . $br;
         echo 'Http Code : ' . $e->httpCode . $br;
         echo 'code : ' . $e->code . $br;
         echo 'message : ' . $e->message . $br;
         echo 'Response JSON : ' . $e->json . $br;
         echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
         echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
         echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
     } catch (APIConnectionException $e) {
         echo 'Push Fail: ' . $br;
         echo 'Error Message: ' . $e->getMessage() . $br;
         // response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
         echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
     }
 }
Example #4
0
 public function createJpusher()
 {
     JPushLog::setLogHandlers(array(new StreamHandler($this->getLogPath() . '/jpush.log', Logger::DEBUG)));
     return new JPushClient($this->appKey, $this->appSecret);
 }
 public function request($url, $data, $header, $method = 'POST')
 {
     $logger = JPushLog::getLogger();
     $logger->debug("Send " . $method, array("method" => $method, "uri" => $url, "headers" => $header, "body" => $data));
     $request = null;
     if ($method === 'POST') {
         $request = Request::post($url);
     } else {
         if ($method == 'DELETE') {
             $request = Request::delete($url);
         } else {
             $request = Request::get($url);
         }
     }
     if (!is_null($data)) {
         $request->body($data);
     }
     $request->addHeaders($header)->authenticateWith($this->appKey, $this->masterSecret)->timeout(self::READ_TIMEOUT);
     $response = null;
     for ($retryTimes = 0;; $retryTimes++) {
         try {
             $response = $request->send();
             break;
         } catch (ConnectionErrorException $e) {
             if (strpos($e->getMessage(), '28')) {
                 throw new APIConnectionException("Response timeout. Your request has probably be received by JPush Server,please check that whether need to be pushed again.", true);
             }
             if (strpos($e->getMessage(), '56')) {
                 // resolve error[56 Problem (2) in the Chunked-Encoded data]
                 throw new APIConnectionException("Response timeout, maybe cause by old CURL version. Your request has probably be received by JPush Server, please check that whether need to be pushed again.", true);
             }
             if ($retryTimes >= $this->retryTimes) {
                 throw new APIConnectionException("Connect timeout. Please retry later.");
             } else {
                 $logger->debug("Exception - Message:" . $e->getMessage() . ", Code:" . $e->getCode());
                 $logger->debug("Retry again send " . $method, array("method" => $method, "uri" => $url, "headers" => $header, "body" => $data, "retryTimes" => $retryTimes + 1));
             }
         }
     }
     return $response;
 }
Example #6
0
function JPushInit()
{
    JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
    return new JPushClient(JPUSH_APIKEY, JPUSH_SECRET);
}
 /**
  * type 1 3 发给商户
  * type 2 发给用户
  * type 4 both
  * user = 0 用户 =2 商户
  */
 public function __construct()
 {
     vendor("Jpush.autoload");
     \JPush\JPushLog::setLogHandlers(array(new \Monolog\Handler\StreamHandler(RUNTIME_PATH . '/Logs/App/jpush.log', \Monolog\Logger::DEBUG)));
 }
Example #8
0
 public function push()
 {
     if ($this->param['type'] === null || $this->param['ispushservice'] === null || $this->param['phone'] === null || $this->param['content'] === null || $this->param['shop_id'] === null) {
         return $this->returnResult($this->DATAERROR, '数据错误!');
     }
     if (strlen($this->param['phone']) != 11) {
         return $this->returnResult($this->DATAERROR, '数据错误!');
     }
     if ($this->param['phone'] == '') {
         return $this->returnResult($this->DATAERROR, '数据错误!');
     }
     $userinfo = UserBase::findFirst("user_account = " . $this->param['phone']);
     if (!$userinfo) {
         return $this->returnResult($this->DATAERROR, '数据错误!');
     }
     $user_id = $userinfo->user_id;
     $usermessage = new UserMessage();
     $usermessage->user_id = $user_id;
     if ($this->param['ispushservice'] == 1) {
         $usermessage->is_read_b = 0;
     } else {
         $usermessage->is_read_b = 1;
     }
     $usermessage->message_content = $this->param['content'];
     $usermessage->push_type = $this->param['type'];
     $usermessage->message_state = 1;
     $usermessage->shop_id = $this->param['shop_id'];
     $usermessage->from_user_name = 1;
     $usermessage->from_user_cover = 1;
     $this->db->begin();
     $message_status = $usermessage->save();
     //添加数据到user_message
     if (!$message_status) {
         $this->db->rollback();
         return $this->returnResult($this->MYSQLERROR, "数据库错误");
     }
     /*推送至APP代码*/
     $config = $this->di->get('appConfig')->jpush;
     $app_key = $config->app_key;
     $master_secret = $config->master_secret;
     JPushLog::setLogHandlers([new StreamHandler('jpush.log', Logger::DEBUG)]);
     $client = new JPushClient($app_key, $master_secret);
     //easy push
     try {
         $result = $client->push()->setPlatform(M\all)->setAudience(M\alias([$user_id]))->setNotification(M\notification($this->param['content']))->setOptions(M\options(null, null, null, true, null))->send();
         if ($result->isOk === true) {
             $this->db->commit();
             return $this->returnResult($this->SUCCESS, "推送成功!");
         } else {
             $this->db->rollback();
             return $this->returnResult($this->PUSHFAIL, "推送失败!");
         }
     } catch (APIRequestException $e) {
         /* echo 'Push Fail.' . '<br>';
          		 echo 'Http Code : ' . $e->httpCode . '<br>';
          		echo 'code : ' . $e->code . '<br>';
          		echo 'message : ' . $e->message . '<br>';
          		echo 'Response JSON : ' . $e->json . '<br>';
          		echo 'rateLimitLimit : ' . $e->rateLimitLimit . '<br>';
          		echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . '<br>';
          		echo 'rateLimitReset : ' . $e->rateLimitReset . '<br>';
          		exit; */
         $this->db->rollback();
         return $this->returnResult($this->PUSHFAIL, "推送失败!");
     } catch (APIConnectionException $e) {
         $this->db->rollback();
         return $this->returnResult($this->PUSHFAIL, "推送失败!");
     }
     /*the end*/
 }