Custom.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Notifications;
  3. use App\Channels\BarkChannel;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Notifications\Messages\MailMessage;
  7. use Illuminate\Notifications\Notification;
  8. use NotificationChannels\Telegram\TelegramChannel;
  9. use NotificationChannels\Telegram\TelegramMessage;
  10. class Custom extends Notification implements ShouldQueue
  11. {
  12. use Queueable;
  13. private string $title;
  14. private string $content;
  15. public function __construct(string $title, string $content)
  16. {
  17. $this->title = $title;
  18. $this->content = $content;
  19. }
  20. public function via($notifiable): array
  21. {
  22. return $notifiable ?? ['mail', BarkChannel::class, TelegramChannel::class];
  23. }
  24. public function toMail($notifiable): MailMessage
  25. {
  26. return (new MailMessage)
  27. ->subject($this->title)
  28. ->markdown('mail.custom', ['content' => $this->content]);
  29. }
  30. public function toCustom($notifiable): array
  31. {
  32. return [
  33. 'title' => $this->title,
  34. 'content' => $this->content,
  35. ];
  36. }
  37. public function toTelegram($notifiable): TelegramMessage
  38. {
  39. return TelegramMessage::create()
  40. ->token(sysConfig('telegram_token'))
  41. ->content($this->content);
  42. }
  43. public function toBark($notifiable): array
  44. {
  45. return [
  46. 'title' => $this->title,
  47. 'content' => $this->content,
  48. 'group' => '自定义信息',
  49. 'icon' => asset('assets/images/notification/custom.png'),
  50. 'sound' => 'newmail',
  51. 'url_type' => 'markdown',
  52. ];
  53. }
  54. }