Custom.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. private array $channels;
  16. public function __construct(string $title, string $content, array $channels = ['mail', BarkChannel::class, TelegramChannel::class])
  17. {
  18. $this->title = $title;
  19. $this->content = $content;
  20. $this->channels = $channels;
  21. }
  22. public function via($notifiable): array
  23. {
  24. return $this->channels;
  25. }
  26. public function toMail($notifiable): MailMessage
  27. {
  28. $emailAddress = config('mail.from.address');
  29. $atSignPosition = strpos($emailAddress, '@');
  30. if ($atSignPosition !== false) {
  31. $domain = substr($emailAddress, $atSignPosition + 1);
  32. $emailAddress = 'no-reply@'.$domain;
  33. }
  34. return (new MailMessage)
  35. ->from($emailAddress)
  36. ->subject($this->title)
  37. ->markdown('mail.custom', ['content' => $this->content]);
  38. }
  39. public function toCustom($notifiable): array
  40. {
  41. return [
  42. 'title' => $this->title,
  43. 'content' => $this->content,
  44. ];
  45. }
  46. public function toTelegram($notifiable): TelegramMessage
  47. {
  48. return TelegramMessage::create()
  49. ->token(sysConfig('telegram_token'))
  50. ->content($this->content);
  51. }
  52. public function toBark($notifiable): array
  53. {
  54. return [
  55. 'title' => $this->title,
  56. 'content' => $this->content,
  57. 'group' => trans('common.bark.custom'),
  58. 'icon' => asset('assets/images/notification/custom.png'),
  59. 'sound' => 'newmail',
  60. 'url_type' => 'markdown',
  61. ];
  62. }
  63. }