流程

触发推送
/** @var PluginHelp $help */
$help = PluginHelp::findOrFail($id);
$type = 'announce';
if ($help->cat_id == site('activity_category')) {
$type = 'activity';
}
\Notification::send(new AccountFront(), new HelpNotification($type, $help));
Notification
class HelpNotification extends Notification implements ShouldQueue, Jpush, AliPush
{
use SerializesModels, Queueable;
/**
* @var
*/
protected $type;
/**
* @var
*/
protected $help;
/**
* Create a new notification instance.
* ArticleAndActivity constructor.
* @param $type
* @param PluginHelp $help
*/
public function __construct($type, $help)
{
$this->type = $type;
$this->help = $help;
}
/**
* Get the notification's delivery channels.
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [JpushChannel::class, AliPushChannel::class];
}
public function toJpush()
{
// 获取 jpush 注册码
/** @var Collection $jpushIds */
$jpushIds = AppInit::where('jpush_id', '!=', '')
->lists('jpush_id');
$ids = $jpushIds->toArray();
if (!$ids) {
return false;
}
$extras = [
'head_pic' => '',
'nickname' => '',
'order_no' => '',
'content' => '',
'type' => $this->type,
'title' => $this->help->help_title,
'url' => route_url('app.help', '', ['id' => $this->help->id]),
'id' => $this->help->id,
'cat_id' => $this->help->cat_id,
];
return [
'broadcast_type' => 'registration_id',
'registration_ids' => $ids,
'title' => $this->help->help_title,
'extras' => $extras,
];
}
public function toAliPush()
{
$extras = [
'type' => $this->type,
'title' => $this->help->help_title,
'url' => route_url('app.help', '', ['id' => $this->help->id]),
'id' => $this->help->id,
'cat_id' => $this->help->cat_id,
];
return [
'broadcast_type' => 'ALL',
'title' => $this->help->help_title,
'content' => mb_substr(strip_tags($this->help->content), 0, 30, 'utf-8'),
'extras' => $extras,
];
}
}
通过极光进行推送
生成极光推送实例
/**
* 极光推送实例
* @package App\Lemon\Dailian\Application
*/
class JPush
{
/**
* @var JPushClient
*/
protected static $instance;
/**
* Create a new instance of this singleton.
* @return JPushClient
*/
final public static function instance()
{
return isset(static::$instance)
? static::$instance
: static::$instance = self::init();
}
/**
* @return JPushClient
*/
protected static function init()
{
$app_key = config('services.jpush.key');
$master_secret = config('services.jpush.secret');
return (new JPushClient($app_key, $master_secret, null));
}
}
极光推送频道
/**
* 极光推送频道
* @package App\Channels
*/
class JpushChannel
{
/**
* The Jpush client manager.
* @var \JPush\Client
*/
protected $clientManager;
/**
* Create a new JPush instance.
*/
public function __construct()
{
$this->clientManager = JPush::instance();
}
/**
* Send the given notification.
* @param mixed $notifiable
* @param Notification| JpushNotify $notification
*/
public function send($notifiable, Notification $notification)
{
$notify = $notification->toJpush();
if (!$notify) {
return;
}
$title = $notify['title'] ?? '';
$extras = $notify['extras'] ?? '';
$broadcast_type = $notify['broadcast_type'] ?? '';
$registration_ids = $notify['registration_ids'] ?? '';
if (!$registration_ids) {
return;
}
try {
$client = $this->clientManager->push()
->setPlatform('all')
->addTag([env('APP_ENV')]);
switch ($broadcast_type) {
case 'registration_id':
$client->addRegistrationId($registration_ids);
break;
}
$client->iosNotification($title, [
'sound' => 'sound.caf',
'category' => 'jiguang',
'extras' => $extras,
])
->androidNotification($extras, [
'title' => '易代练',
'extras' => $extras,
])
->options(array(
'apns_production' => (env('APP_ENV') == 'production'),
))
->send();
} catch (\Exception $e) {
\Log::debug($e->getMessage());
}
}
}