//方法
use JPush\Client;
use Ws\Http\Request;
use Ws\Http\Request\Body;
protected function Jpush_user($phone, $alert, $extras)
{
$client = new Client(config('jpush')['user']['app_key'], config('jpush')['user']['master_secret'], null);
$pusher = $client->push()->addAlias($phone)->setPlatform(['ios', 'android'])->iosNotification($alert, [
'extras' => $extras,
])->androidNotification($alert, [
'extras' => $extras,
]);
if (config('iOS_push_debug') === false) {
$pusher->setOptions(null, null, null, true);
}
try {
$pusher->send();
} catch (\Exception $e) {
trace('乘客端推送失败:' . $e->getMessage());
}
$this->TPNS('user', $phone, $alert, $extras);
}
protected function TPNS($config, $phone, $alert, $extras, $sound = '')
{
if ($config === 'user') {
return;
}
$config = config('tpns')[$config];
$point = 'https://api.tpns.tencent.com/v3/push/app';
$http = Request::create();
$custom = json_encode($extras);
$param = [
'audience_type' => 'account',
'account_list' => [$phone],
'message' => [
'title' => $alert,
'content' => '',
],
'message_type' => 'notify',
];
// Android 推送
$param_a = $param;
$param_a['message']['android'] = [
'custom_content' => $custom,
];
$resp = $http->post($point, [
'Authorization' => 'Basic ' . base64_encode($config['Android']['AccessID'] . ':' . $config['Android']['SecretKey']),
], Body::json($param_a));
if ($resp->body->ret_code !== 0) {
trace('腾讯云移动推送Android推送失败:' . $resp->body->err_msg);
}
// iOS推送
$param_i = $param;
$param_i['message']['ios'] = [
'aps' => [
'alert' => [
'subtitle' => $alert,
],
],
'sound' => $sound,
'custom_content' => $custom,
];
if (config('iOS_push_debug')) {
$param_i['environment'] = 'dev';
} else {
$param_i['environment'] = 'product';
}
$resp = $http->post($point, [
'Authorization' => 'Basic ' . base64_encode($config['iOS']['AccessID'] . ':' . $config['iOS']['SecretKey']),
], Body::json($param_i));
if ($resp->body->ret_code !== 0) {
trace('腾讯云移动推送iOS推送失败:' . $resp->body->err_msg);
}
}
//使用
$this->Jpush_user($user->phone, $data['title'], ['type' => 0, 'message_id' => $message_id]);
极光推送。。
最新推荐文章于 2024-09-17 21:04:05 发布