montopolis / mynabird
此包已被 弃用,不再维护。未建议替代包。
Laravel/Nova 的应用程序内警报包。
1.0.0
2022-12-19 09:41 UTC
Requires
- php: ^8.0
- illuminate/database: ^9.0
- illuminate/http: ^9.0
- illuminate/support: ^9.0
- nesbot/carbon: >=2.37
README
Laravel/Nova 包,用于 Facebook 风格的警报。
目前支持 Pusher,当接收到新的警报时进行实时通知(并动态更新未读计数器)。
安装
此包可以通过 Composer 安装。
composer require montopolis/mynabird
确保所有供应商文件已发布且迁移运行
php artisan vendor:publish --provider="Montopolis\Mynabird\MynabirdServiceProvider"
php artisan migrate
假设您正在使用 Nova,您还需要发布 Nova 的供应商模板(如果您尚未这样做),以便可以插入 Mynabird 部分内容
php artisan nova:publish
...然后在这 Nova 布局模板中添加这两行
// in file: resources/views/vendor/nova/layout.blade.php ... <dropdown class="ml-auto h-9 flex items-center dropdown-right"> @include('nova::partials.user') </dropdown> @include('mynabird::alert-feed') <!-- AND THIS LINE HERE --> ... <!-- Build Nova Instance --> <script> window.Nova = new CreateNova(config) </script> <!-- Alerts --> @include('mynabird::javascript') <!-- INCLUDE THIS LINE HERE --> ...
如果您想使用实时通知,请确保通过 config/broadcasting.php
和/或 .env 文件配置了 Pusher;例如
# Set the broadcast driver to send events to Pusher: BROADCAST_DRIVER=pusher # Add the pusher config, which you can find/configure via the dashboard: https://dashboard.pusher.com PUSHER_APP_ID=aaabbbcccdddeeefffgg PUSHER_APP_KEY=hhhiiijjjkkklllmmmnn PUSHER_APP_SECRET=1000001 PUSHER_APP_CLUSTER=mt1
或者如果您想禁用实时通知,您可以通过 Mynabird 配置来实现
<?php // in config/mynabird.php return [ 'should_broadcast' => false, ];
如果禁用了广播,用户需要刷新才能看到新的警报。
使用方法
您可以使用 AlertRepository
触发警报,它(假设已加载 MynabirdServiceProvider)可以通过 Laravel IoC 容器解析
<?php declare(strict_types=1); namespace App\Console; use Montopolis\Mynabird\DataObjects\Alert; use Montopolis\Mynabird\Interfaces\AlertsRepository; class BroadcastAlert extends Command { protected $signature = 'app:broadcast-alert'; protected $description = 'Send an alert to all users.'; protected $alertsRepository; public function __construct(AlertsRepository $alertsRepository) { parent::__construct(); $this->alertsRepository = $alertsRepository; } public function handle() { // Create an alert object: Montopolis\Mynabird\DataObjects\Alert $alert = new Alert( null, $title, $body, $url, $level, null, // set to user ID for individual notification true // set to `true` for broadcast message ); // Send the alert: $this->alertsRepository->store($alert); } }
鸣谢
- 用户界面和样式表改编自 coreproc/nova-notification-feed。
许可协议
此软件使用 MIT 许可协议。请参阅 许可文件 了解更多信息。