一定要注意 极光的环境
// 推送环境 开发false 生产true
$this->apns_production = false;
public function setJpush($uid = 0, $money, $id = 0) {
$user = M('users')->where(array('id' => $uid))->find();
if (empty($user) || strlen($user['registration_id']) == 0) {
return false;
}
/**
* message 数据
*/
$title = '交易通知!';
$content = '收益到账'.$money.'元';
$platform = array('ios', 'android');
$alert = $content;
$regId[] = $user['registration_id'];
$sendData = array(
'alert' => $alert,
'title' => $title,
'content' => $content,
'regId' => $regId,
'platform' => $platform,
'id' => $id, // 发送内容关联id
'uid' => $uid, // 发送人
'msg_type' => 2 // 交易消息
);
$re = $this->jpush->send($sendData);
if ($re['code'] !== 0) {
return false;
// $this->error($re['mg']);
}
return true;
}
<?php
namespace Common\Controller;
use Common\Controller\AppframeController;
use JPush\Client;
class JpushsendController extends AppframeController{
protected $client;
protected $cid;
protected $apns_production;
protected $rqdata = [
'alert',
'title',
'content',
'regId',
'platform',
'id',
'uid',
'msg_type'
];
protected $msg_type = [
'1' => 11, // 11跳转系统消息 app跳转标识
'2' => 12, // 12跳转交易消息
];
public function __construct() {
require APP_PATH.'Admin/JPush/autoload.php';
$config = M("config_private")->where("id='1'")->field('jpush_key, jpush_secret, jpush_sandbox')->find();
$app_key = trim($config['jpush_key']);
$master_secret = trim($config['jpush_secret']);
// 推送环境 开发false 生产true
$this->apns_production = false;
if ((int)$config['jpush_sandbox']) {
$this->apns_production = true;
}
// 初始化
$this->client = new \JPush\Client($app_key, $master_secret, './jpush.log');
}
/**
* 发送请求
*/
public function send($sendData) {
$res = ['code'=> 0,'mg'=>'成功!'];
foreach ($sendData as $k => $v) {
if (!in_array($k, $this->rqdata)) {
$res['code'] = 1001;
$res['mg'] = '缺少必要的参数!'.$k;
return $res;
}
$$k = $v;
};
$ios_notification = array(
'sound' => $title,
'badge' => +1,
'category' => 'jiguang',
'extras' => array(
'msg_type' => $this->msg_type[$msg_type],
'jiguang'
),
);
$android_notification = array(
'title' => $title,
'builder_id' => 2,
'extras' => array(
'msg_type' => $this->msg_type[$msg_type],
'jiguang'
),
);
$message = array(
'title' => $title,
'content_type' => 'text',
'extras' => array(
'msg_type' => $this->msg_type[$msg_type],
'jiguang'
),
);
$options = array(
'apns_production' => $this->apns_production,
);
$this->setCid();
try {
$response = $this->client->push()
->setCid($this->cid)
->setPlatform($platform) // array('ios', 'android');
// ->addTag($tag)
->addRegistrationId($regId) // 目标设备标识
->iosNotification($alert, $ios_notification)
->androidNotification($alert, $android_notification)
->message($content, $message)
->options($options)
->send();
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
// $this->error($e->xdebug_message);
$res['code'] = 1;
$res['mg'] = $e->xdebug_message;
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
// $this->error($e->xdebug_message);
$res['code'] = 1;
$res['mg'] = $e->xdebug_message;
}
$data = [
'notice_id' => $id,
'user_id' => $uid,
'platform' => json_encode($platform),
'content' => $content,
'msg_type' => $msg_type,
'registrationids' => json_encode($regId),
'status' => $res['code'],
'mg' => $res['mg'],
'time' => time()
];
$config = M("jpush_log")->add($data);
return $res;
}
public function setCid() {
$PushPayload = new \JPush\PushPayload($this->client);
// 获取cid
$getCid = $PushPayload->getCid(1,'push');
$this->cid = time().mt_rand();
if ($getCid['body']['cidlist'][0]) {
$this->cid = $getCid['body']['cidlist'][0];
}
}
}