我想在我的应用程序中进行实时通知。我在我的应用程序中使用 Laravel 广播通知、Laravel Echo 和 Laravel Echo Server。在本地通知中监听所有用户,但在服务器中,如果为 5 个用户添加通知,则监听 2 到 3 个用户,否则用户登录保存在 laravel.log 文件中.
代码
// 控制器文件
$users = User::whereHas('dealerRoles', function ($query) {
$query->where('name', DealerRole::DEALERSHIP_DIRECTOR);
$query->orWhere('name', DealerRole::DEALERSHIP_PRINCIPAL);
$query->orWhere('name', DealerRole::DEALERSHIP_MANAGER);
})->whereHas('dealerships', function ($query) use ($listing) {
$query->where('dealerships.id', $listing->owner_id);
})->get();
\Notification::send($users, (new ListingApproved($listing)));
// 通知文件(ListingApproved.php)
class ListingApproved extends Notification implements ShouldQueue{
use Queueable;
private $listing;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Listing $listing)
{
$this->listing = $listing;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toArray($notifiable)
{
return [
'listing_id' => $this->listing->id,
'message' => 'Your Listing has been approved Successfully.'
];
}
}
// laravel-echo-server.json
{
"authHost": ["http://dealers.cc/", "http://admin.videocarsales.cc/"],
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "31071dab508dc1bb",
"key": "8262eb7c6589432f457ad25b83d4cb5a"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "7878",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "http://localhost:80",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
// 监听通知的前端
Echo.private('App.Domain.User.User.' + '{{ auth()->user()->id }}')
.notification((notification) => {
console.log('yes');
Messenger().post({
message: 'Your Listing las been approved Successfully.',
type: 'success',
showCloseButton: true
});
});
//laravel.log
[2018-06-13 08:13:09] production.INFO: Broadcasting [Illuminate\Notifications\Events\BroadcastNotificationCreated] on channels [private-App.Domain.User.User.49] with payload:
{
"listing_id": 1,
"message": "Your Listing has been approved Successfully.",
"id": "acb1f428-0116-43a4-88cd-53e35089084a",
"type": "App\\Notifications\\ListingApproved",
"socket": null}
请帮我。提前致谢!