laravel-learn-bbs/app/Listeners/PushNotification.php

49 lines
1.0 KiB
PHP
Raw Normal View History

2018-06-16 02:18:41 +00:00
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\DatabaseNotification;
use JPush\Client;
class PushNotification implements ShouldQueue
{
protected $client;
/**
* Create the event listener.
* @param Client $client
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* Handle the event.
*
* @param DatabaseNotification $notification
* @return void
*/
public function handle(DatabaseNotification $notification)
{
if (app()->environment('local')) {
return;
}
$user = $notification->notifiable;
if (!$user->registration_id) {
return;
}
// 推送消息
$this->client->push()
->setPlatform('all')
->addRegistrationId($user->registration_id)
->setNotificationAlert(strip_tags($notification->data['reply_content']))
->send();
}
}