步骤:
官方github SDK:
https://github.com/jpush/jpush-api-php-client
1、账号配置:
极光官网: https://www.jiguang.cn/
2、下载sdk引入项目
我是用composer,项目为thinkphp5框架,直接执行:
composer require jpush/jpush
3、编写推送类实现推送:
/**
* desc 用户绑定极光推送唯一标识id
*/
public function addBindJpush()
{
$json = $this->request->param();
$params = json_decode($json['data'], true);
$params['uid'] = $this->uid;
// 判断是安卓还是ios
$platform = $json['_platform'];
$type_ = 1;
if ($platform == 'android') {
$type_ = 1;
} elseif ($platform == 'ios') {
$type_ = 2;
}
if (empty($params['registration_id']) || empty($params['uid'])) {
throw new Exception(10009);
}
$info = UserJpush::where(['uid' => $params['uid']])->find();
if (!empty($info)) {
if ($info['registration_id'] != $params['registration_id']) {
UserJpush::where(['uid' => $params['uid']])->update(['registration_id' => $params['registration_id'], 'type' => $type_, 'up_time' => date('Y-m-d H:i:s')]);
}
$this->returnmsg(200, 'success', []);
} else {
// 添加绑定
$res = UserJpush::create([
'uid' => $params['uid'],
'registration_id' => $params['registration_id'],
'add_time' => date('Y-m-d H:i:s'),
'type' => $type_,
]);
$this->returnmsg(200, 'success', $res);
}
}
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/7 0007
* Time: 14:04
*/
namespace Api;
use app\common\model\UserJpush;
use think\Exception;
use JPush\Client;
class JPush{
/**
* 极光推送
* @param int $type 1安卓 2 ios
* @param string $title 标题
* @param string $push_type 推送类型:1全部,2 registration_id ,3 别名
* @param string $message_content 内容
* @param array $data 推送目标,例如:1为 all,2为用户id[ 122 ,123 ],3为别名id数组[]
* @param int $item_id 扩展id
* @return mixed
*/
public static function push($type = 1,$title,$message_content,$push_type = 2,$data,$item_id = 0){
if(empty($title)) return false;
$config_private = \config('jpush_config');
$app_key = $type == 1 ? $config_private['and_jpush_key'] : $config_private['ios_jpush_key'];
$master_secret = $type == 1 ? $config_private['and_jpush_secret'] : $config_private['ios_jpush_secret'];
$client = new Client($app_key, $master_secret);
if ($push_type == 1){
$data = 'all';
}elseif ($push_type == 2){
$list = UserJpush::where(['uid'=>['in',$data]])->select()->toArray();
$data = [];
if ($list){
$data = array_unique(array_column($list,'registration_id'));
}
}else{
return true;
}
try {
$client = $client->push()->setPlatform(array('ios', 'android'));
if ($push_type == 1){
$client = $client->addAllAudience($data);
}elseif ($push_type == 2){
$client = $client->addRegistrationId($data);
}
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
// ->addAlias($alias)
// ->addTag(array('tag1', 'tag2'))
// ->addAllAudience()
$res = $client->setNotificationAlert($title)
->iosNotification(['title'=>$title,'body'=>$message_content], array(
'sound' => 'sound.caf',
// 'badge' => '+1',
// 'content-available' => true,
// 'mutable-content' => true,
'extras' => array(
'id' => $item_id,
),
))
->androidNotification($title, array(
'title' => $message_content,
// 'builder_id' => 2,
'extras' => array(
'id' => $item_id,
),
))
->message($message_content, array(
'title' => $title,
// 'content_type' => 'text',
'extras' => array(
'id' => $item_id,
),
))
->options(array(
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
// 这里设置为 100 仅作为示例
// 'sendno' => 100,
// time_to_live: 表示离线消息保留时长(秒),
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
// 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例
// 'time_to_live' => 1,
// apns_production: 表示APNs是否生产环境,
// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
'apns_production' => true,
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
// 这里设置为 1 仅作为示例
// 'big_push_duration' => 1
))
->setSmsMessage(array(
'delay_time' => 60,
'signid' => 154,
'temp_id' => 1,
'temp_para' => array(
'code' => 357
),
'active_filter' => false
))
->send();
} catch (Exception $e) {
// try something else here
$ress['code'] = $e->getCode();
$ress['msg'] = $e->getMessage();
$data = ['data'=>$data,'res'=>$ress];
add_debug_log($data,'addJpush','推送失败');
}
return true;
}
}
/**
* 极光推送
* @param int $type 1安卓 2 ios
* @param string $title 标题
* @param string $push_type 推送类型:1全部,2 registration_id ,3 别名
* @param string $message_content 内容
* @param array $data 推送目标,例如:1为 all,2为用户id[ 122 ,123 ],3为别名id数组[]
* @param int $item_id 扩展id
* @return mixed
* * 例如:addJpush(2,'测试-标题2','测试推送-内容',2,['110134','7232'],'6547');
*/
function addJpush($type = 1,$title,$message_content,$push_type = 2,$data,$item_id = 0){
$push_res = JPush::push( $type,$title,$message_content,$push_type,$data,$item_id);
return $push_res;
}
addJpush(2, '认证通过通知', '恭喜您已提交认证...', 2, [$uid], $uid);
end