mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
feat(#36): add Mail notification on Low Battery
This commit is contained in:
parent
1122764333
commit
33e1f99fad
11 changed files with 375 additions and 0 deletions
71
app/Notifications/BatteryLow.php
Normal file
71
app/Notifications/BatteryLow.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Notifications\Channels\WebhookChannel;
|
||||
use App\Notifications\Messages\WebhookMessage;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class BatteryLow extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
private Device $device;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*/
|
||||
public function __construct(Device $device)
|
||||
{
|
||||
$this->device = $device;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['mail', WebhookChannel::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*/
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
return (new MailMessage)->markdown('mail.battery-low', ['device' => $this->device]);
|
||||
}
|
||||
|
||||
public function toWebhook(object $notifiable)
|
||||
{
|
||||
return WebhookMessage::create()
|
||||
->data([
|
||||
'topic' => config('services.webhook.notifications.topic', 'battery.low'),
|
||||
'message' => "Battery below {$this->device->battery_percent}% on device: {$this->device->name}",
|
||||
'device_id' => $this->device->id,
|
||||
'device_name' => $this->device->name,
|
||||
'battery_percent' => $this->device->battery_percent,
|
||||
|
||||
])
|
||||
->userAgent(config('app.name'))
|
||||
->header('X-TrmnlByos-Event', 'battery.low');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
'device_name' => $this->device->name,
|
||||
'battery_percent' => $this->device->battery_percent,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue