参考:http://docs.jpush.io/server/php_sdk/ Thinkphp推送类(需要下载vendor)
代码语言:javascript复制push()
->setPlatform(MPlatform('ios'))
->setAudience(Mall)
->setNotification(Mnotification($message))
// ->printJSON()
->send();
return $result->json;
} catch (APIRequestException $e) {//推送失败调试时候可以打开
// $br = 'n';
// 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;
return false;
} catch (APIConnectionException $e) {
// $br = "n";
// echo 'Push Fail: ' . $br;
// echo 'Error Message: ' . $e->getMessage() . $br;
// echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
return false;
}
return false;
}
/**
* 推送信息到指定用户 (备注:一次最多只能推送1000个RegID)
* @param String $message 消息
* @param Array $users 用户编号
* @param Boolean $all 是否推送该用户所有设备 默认推送所有 否则推送最新的一个
* @return boolean
*/
public static function sendToUser($message, $users, $all = true) {
if(empty($message) || empty($users) || !is_array($users)) {
return false;
}
$audience = $list = array();
$map['user_id'] = array('in', $users);
if($all) {
$list = M(self::USER_TABLE)->field(self::USER_REGID_FIELD)->where($map)->select();
} else {
$list = M(self::USER_TABLE)->field(self::USER_REGID_FIELD)->where($map)->limit(1)->order(self::USER_TABLE_ORDER)->select();
}
if($list) {
foreach ($list as $v) {
if(trim($v[self::USER_REGID_FIELD])) {
$audience[] = $v[self::USER_REGID_FIELD];
}
}
}
if(!$audience && $audience[0]) {
return false;
}
return self::sendToRegistrationId($message, $audience);
}
/**
* 推送消息到指定IOS设备 最多一次推送1000
* 备注参考:http://docs.jpush.io/server/rest_api_v3_push/
*/
public static function sendToRegistrationId($message,$audience) {
if(empty($message) || empty($audience) || !is_array($audience)) {
return false;
}
try {
$client = new JPushClient(self::APP_KEY, self::MASTER_SECRET);
$result = $client->push()
->setPlatform(Mplatform('ios'))
->setNotification(Mnotification($message))
->setAudience(Mregistration_id($audience))
//->printJSON()
->send();
return $result->json;
} catch (APIRequestException $e) {
// $br = 'n';
// 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;
return false;
} catch (APIConnectionException $e) {
// $br = "n";
// echo 'Push Fail: ' . $br;
// echo 'Error Message: ' . $e->getMessage() . $br;
// echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
// return false;
}
return false;
}
/**
* 向消息队列压入推送任务 1 推送所有 2推送指定设备 3推送指定用户
* @param String $message 推送内容
* @param Int $type 推送类型
* @param Array $ext 推送用户编号或RegID
* @return boolean
*/
public function pushMessage($message, $type = self::SEND_TO_ALL, $ext = array()) {
if(empty($message) || !in_array($type, array(self::SEND_TO_ALL,self::SEND_TO_REG,self::SEND_TO_USER))) {
return false;
}
try {
$redis = new Redis;
$redis->connect('127.0.0.1', 6379);
$data['message'] = $message;
$data['type'] = $type;
$data['ext'] = $ext;
$pushData = json_encode($data);
return $redis->lPush(self::REDIS_PUSH_MESSAGE_KEY, $pushData);//队列左边添加元素
} catch (Exception $e) {
// echo $e->getMessage();
return false;
}
return false;
}
}