228 lines
8.1 KiB
PHP
228 lines
8.1 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: Administrator
|
||
* Date: 2019/8/21
|
||
* Time: 14:25
|
||
*/
|
||
|
||
namespace app\common\service;
|
||
|
||
use JPush\Client as JPush;
|
||
use JPush\Exceptions\APIConnectionException;
|
||
use JPush\Exceptions\APIRequestException;
|
||
use think\facade\Log;
|
||
|
||
class JpushServer
|
||
{
|
||
public $client;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->client = new JPush(config('app_key'),config('master_secret'),config('base_url'));
|
||
}
|
||
|
||
/**
|
||
* @param $content string 推送内容
|
||
* @param $alias array 推送对象
|
||
* @param $alert string 消息弹出内容
|
||
* @param $param array 参数
|
||
* @return array
|
||
*/
|
||
public function pushMsg($content, $alias, $alert, $param)
|
||
{
|
||
try{
|
||
$this->client->push()
|
||
|
||
->setPlatform(['ios', 'android'])
|
||
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
|
||
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
|
||
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
|
||
|
||
->addAlias($alias)
|
||
//->addTag(array('tag1', 'tag2'))
|
||
// ->addRegistrationId($registration_id)
|
||
|
||
->setNotificationAlert($alert)
|
||
//->setAudience('all')
|
||
->iosNotification(array("body" => $content, 'title' => $alert), array(
|
||
'sound' => 'sound.caf',
|
||
// 'badge' => '+1',
|
||
'content-available' => true,
|
||
// 'mutable-content' => true,
|
||
'category' => 'jiguang',
|
||
'extras' => $param,
|
||
))
|
||
->androidNotification($content, array(
|
||
'title' => $alert,
|
||
// 'builder_id' => 2,
|
||
'extras' => $param,
|
||
))
|
||
// ->message('message content', array(
|
||
// 'title' => 'hello jpush',
|
||
// 'extras' => $param,
|
||
// ))
|
||
->options(array(
|
||
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
|
||
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
|
||
// 这里设置为 100 仅作为示例
|
||
|
||
//'sendno' => 100,
|
||
|
||
// time_to_live: 表示离线消息保留时长(秒),
|
||
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
|
||
// 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
|
||
// 这里设置为 1 仅作为示例
|
||
|
||
'time_to_live' => 864000,
|
||
|
||
// apns_production: 表示APNs是否生产环境,
|
||
// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
|
||
|
||
'apns_production' => false,
|
||
|
||
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
|
||
// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
|
||
// 这里设置为 1 仅作为示例
|
||
))
|
||
->send();
|
||
}catch (APIConnectionException $e) {
|
||
Log::error($e->getMessage());
|
||
//file_put_contents('../runtime/err.log',json_encode($e->getMessage())."\r\n", FILE_APPEND);
|
||
} catch (APIRequestException $e) {
|
||
Log::error($e->getMessage());
|
||
//file_put_contents('../runtime/err.log',json_encode($e->getMessage())."\r\n", FILE_APPEND);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param $content
|
||
* @param $alert
|
||
* @param $param
|
||
* @param array $platform
|
||
*/
|
||
public function pushAllMsg($content, $alert, $param, $platform = ['ios', 'android'])
|
||
{
|
||
try{
|
||
$this->client->push()
|
||
->setPlatform($platform)
|
||
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
|
||
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
|
||
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
|
||
|
||
//->addAlias($alias)
|
||
//->addTag(array('tag1', 'tag2'))
|
||
// ->addRegistrationId($registration_id)
|
||
|
||
->setNotificationAlert($alert)
|
||
->setAudience('all')
|
||
->iosNotification(array("body" => $content, 'title' => $alert), array(
|
||
'sound' => 'sound.caf',
|
||
// 'badge' => '+1',
|
||
'content-available' => true,
|
||
// 'mutable-content' => true,
|
||
'category' => 'jiguang',
|
||
'extras' => $param,
|
||
))
|
||
->androidNotification($content, array(
|
||
'title' => $alert,
|
||
// 'builder_id' => 2,
|
||
'extras' => $param,
|
||
))
|
||
->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' => config('app_debug'),
|
||
|
||
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
|
||
// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
|
||
// 这里设置为 1 仅作为示例
|
||
|
||
// 'big_push_duration' => 1
|
||
))
|
||
->send();
|
||
}catch (APIConnectionException $e) {
|
||
Log::error($e->getMessage());
|
||
//file_put_contents('../runtime/aaaa.log',json_encode($e)."\r\n", FILE_APPEND);
|
||
} catch (APIRequestException $e) {
|
||
Log::error($e->getMessage());
|
||
//file_put_contents('../runtime/aaaa.log',json_encode($e)."\r\n", FILE_APPEND);
|
||
}
|
||
}
|
||
|
||
|
||
//定时指定人员发送
|
||
public function pushMsgByTime($content, $alias, $alert, $param, $time)
|
||
{
|
||
try{
|
||
$payload = $this->client->push()
|
||
->setPlatform(['ios', 'android'])
|
||
->addAlias($alias)
|
||
->setNotificationAlert($alert)
|
||
->iosNotification(array("body" => $content, 'title' => $alert), array(
|
||
'sound' => 'sound.caf',
|
||
// 'badge' => '+1',
|
||
'content-available' => true,
|
||
// 'mutable-content' => true,
|
||
'category' => 'jiguang',
|
||
'extras' => $param,
|
||
))
|
||
->androidNotification($content, array(
|
||
'title' => $alert,
|
||
// 'builder_id' => 2,
|
||
'extras' => $param,
|
||
))
|
||
->build();
|
||
$this->client->schedule()->createSingleSchedule("每天14点发送的定时任务", $payload, array("time"=>$time));
|
||
}catch (APIConnectionException $e) {
|
||
Log::error($e->getMessage());
|
||
} catch (APIRequestException $e) {
|
||
Log::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
//定时发送
|
||
public function pushAllMsgByTime($content, $alert, $param, $time)
|
||
{
|
||
try{
|
||
$payload = $this->client->push()
|
||
->setPlatform("all")
|
||
->addAllAudience()
|
||
->setNotificationAlert($alert)
|
||
->iosNotification(array("body" => $content, 'title' => $alert), array(
|
||
'sound' => 'sound.caf',
|
||
// 'badge' => '+1',
|
||
'content-available' => true,
|
||
// 'mutable-content' => true,
|
||
'category' => 'jiguang',
|
||
'extras' => $param,
|
||
))
|
||
->androidNotification($content, array(
|
||
'title' => $alert,
|
||
// 'builder_id' => 2,
|
||
'extras' => $param,
|
||
))
|
||
->build();
|
||
$this->client->schedule()->createSingleSchedule("每天14点发送的定时任务", $payload, array("time"=>$time));
|
||
}catch (APIConnectionException $e) {
|
||
Log::error($e->getMessage());
|
||
} catch (APIRequestException $e) {
|
||
Log::error($e->getMessage());
|
||
}
|
||
}
|
||
}
|